简体   繁体   English

如何从 c# 范围内仅生成一个随机值

[英]How to generate only one random value from a range c#

Generate only one random value / int from a range I've run into a problem where when I generate a random value / int from a range it generates like 10 when I only want it to generate one.从一个范围内只生成一个随机值/int我遇到了一个问题,当我从一个范围内生成一个随机值/int 时,它会生成 10,而我只希望它生成一个。 I've tried putting the code that generates the random int in an if statement where a variable is required to be false to run it then change the variable to true inside of the if statement but that doesn't work either.我已经尝试将生成随机 int 的代码放在 if 语句中,其中需要一个变量为 false 才能运行它,然后在 if 语句中将变量更改为 true,但这也不起作用。

random = Random.Range(0, 9);

The line of code above is what I'm using to generate the random int.上面的代码行是我用来生成随机整数的。 I have also tried another method which I'll show below.我还尝试了另一种方法,我将在下面展示。

int buttonValue = Random.Range(minButtons, maxButtons);
//these are the variables being used in the code above
public int maxButtons = 9;
public int minButtons = 1;
int buttonValue;

For reference this is my entire script.作为参考,这是我的整个脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonSystem : MonoBehaviour
{

    // buttons
    GameObject button1;
    GameObject button2;
    GameObject button3;
    GameObject button4;
    GameObject button5;
    GameObject button6;
    GameObject button7;
    GameObject button8;
    GameObject button9;

    // button fronts
    GameObject buttonfront1;
    GameObject buttonfront2;
    GameObject buttonfront3;
    GameObject buttonfront4;
    GameObject buttonfront5;
    GameObject buttonfront6;
    GameObject buttonfront7;
    GameObject buttonfront8;
    GameObject buttonfront9;

    public Material greenLight;
    public Material redLight;

    public int maxButtons = 9;
    public int minButtons = 1;
    bool numGenerated;
    bool isGenerated;
    int[] buttons = new int[9] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int random;

    public void Start()
    {
        // defining buttons
        button1 = GameObject.Find("PARENT_button1");
        button2 = GameObject.Find("PARENT_button2");
        button3 = GameObject.Find("PARENT_button3");
        button4 = GameObject.Find("PARENT_button4");
        button5 = GameObject.Find("PARENT_button5");
        button6 = GameObject.Find("PARENT_button6");
        button7 = GameObject.Find("PARENT_button7");
        button8 = GameObject.Find("PARENT_button8");
        button9 = GameObject.Find("PARENT_button9");

        // defining front buttons
        buttonfront1 = GameObject.Find("buttonfront_1");
        buttonfront2 = GameObject.Find("buttonfront_2");
        buttonfront3 = GameObject.Find("buttonfront_3"); 
        buttonfront4 = GameObject.Find("buttonfront_4");
        buttonfront5 = GameObject.Find("buttonfront_5");
        buttonfront6 = GameObject.Find("buttonfront_6");
        buttonfront7 = GameObject.Find("buttonfront_7");
        buttonfront8 = GameObject.Find("buttonfront_8");
        buttonfront9 = GameObject.Find("buttonfront_9");


        isGenerated = false;
        numGenerated = false;
        generate();
        StartCoroutine(waitbeforeStart());
    }

    IEnumerator waitbeforeStart()
    {
        yield return new WaitForSeconds(1);
        StartCoroutine(ButtonAlgo());
    }

    void generate()
    {
        if (isGenerated == false)
        {
            random = Random.Range(0, 9);
            Debug.Log(random);
            isGenerated = true;
        }
    }

    IEnumerator ButtonAlgo()
    {
        
        yield return new WaitForSeconds(1);

        switch (random)
        {
            case 1:
                button1.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront1.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 2:
                button2.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront2.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 3:
                button3.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront3.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 4:
                button4.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront4.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 5:
                button5.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront5.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 6:
                button6.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront6.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 7:
                button7.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront7.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 8:
                button8.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront8.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
            case 9:
                button9.GetComponentInChildren<Light>().color = new Color32(23, 255, 0, 255);
                buttonfront9.GetComponent<MeshRenderer>().material = greenLight;
                yield break;
        }       
    }
}

Heres an image of the console which is logging the value of finalValue I want it to give 1 value but it gives several这是控制台的图像,它正在记录 finalValue 的值我希望它给出 1 个值,但它给出了几个

what you missed is that: to choose a random index you should start from 0 .您错过的是:要选择一个随机索引,您应该从0开始。

so change your minButtonIndex from 1 to 0 .所以将你的minButtonIndex1更改为0

NOTE: use switch instead of if else statement like:注意:使用 switch 而不是 if else 语句,如:

switch(finalNum)
{
    case 1:
    // run your code when final was equal to 1.
    break;
    // you can add as many case as you want to.
}

Look:看:

you are setting the minButtons and maxButtons after you used them so you should set those before getting the random number.您在使用它们之后设置minButtonsmaxButtons ,因此您应该在获取随机数之前设置它们。

Look like this:看起来像这样:

public int maxButtons = 9;
public int minButtons = 0;
int buttonValue = Random.Range(minButtons, maxButtons);

Ahh i found it i hopefully啊,我找到了,我希望

create a Void for generate a Value but its only generate one when the bool is not true // isGenerated = false by default创建一个 Void 以生成一个值,但它仅在 bool 不为 true 时才生成一个 // isGenerated = false 默认情况下

void generate(){
    if(isGenerated == true){
        // generate one
        isGenerated = true;
    }
}

and when you safed the final result you can set isGenerated to false当您保护最终结果时,您可以将 isGenerated 设置为 false

this is how it should look like at the end这就是它最后的样子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonSystem : MonoBehaviour
{

    // buttons
    GameObject button1;
    GameObject button2;
    GameObject button3;
    GameObject button4;
    GameObject button5;
    GameObject button6;
    GameObject button7;
    GameObject button8;
    GameObject button9;

    // button fronts
    GameObject buttonfront1;
    GameObject buttonfront2;
    GameObject buttonfront3;
    GameObject buttonfront4;
    GameObject buttonfront5;
    GameObject buttonfront6;
    GameObject buttonfront7;
    GameObject buttonfront8;
    GameObject buttonfront9;

    public Material greenLight;
    public Material redLight;

    private color green = new Color32(23, 255, 0, 255);

    public int maxButtons = 9;
    public int minButtons = 1;

    int buttonValue;
    int finalValue;
    bool waitready;
    bool isGenerated;  // <--here is a change
    bool numLock;
    bool numGenerated;
    bool finalCalled;

    int[] buttons = new int[9] {1, 2, 3, 4, 5, 6, 7, 8, 9};

    public void Start()
    {
        // defining buttons
        button1 = GameObject.Find("PARENT_button1");
        button2 = GameObject.Find("PARENT_button2");
        button3 = GameObject.Find("PARENT_button3");
        button4 = GameObject.Find("PARENT_button4");
        button5 = GameObject.Find("PARENT_button5");
        button6 = GameObject.Find("PARENT_button6");
        button7 = GameObject.Find("PARENT_button7");
        button8 = GameObject.Find("PARENT_button8");
        button9 = GameObject.Find("PARENT_button9");

        // defining front buttons
        buttonfront1 = GameObject.Find("buttonfront_1");
        buttonfront2 = GameObject.Find("buttonfront_2");
        buttonfront3 = GameObject.Find("buttonfront_3");
        buttonfront4 = GameObject.Find("buttonfront_4");
        buttonfront5 = GameObject.Find("buttonfront_5");
        buttonfront6 = GameObject.Find("buttonfront_6");
        buttonfront7 = GameObject.Find("buttonfront_7");
        buttonfront8 = GameObject.Find("buttonfront_8");
        buttonfront9 = GameObject.Find("buttonfront_9");

        isGenerated = false; // <-- here is a change
        waitready = false;
        numLock = false;
        numGenerated = false;
        finalCalled = false;
        StartCoroutine(waitbeforeStart());
    }

    void generate(){
        if(isGenerated == true){
            int finalValue = buttons[Random.Range(0, buttons.Length)];
            finalCalled = true;
            numLock = true;
            isGenerated = true;
        }
    }

    IEnumerator waitbeforeStart()
    {
        yield return new WaitForSeconds(1);
        waitready = true;
        StartCoroutine(ButtonAlgo(waitready, numGenerated, numLock, buttons, finalCalled));
    }
    IEnumerator ButtonAlgo(bool waitready, bool numGenerated, bool numLock, int[] buttons, bool finalCalled)
    {
        // buttonAlgostart
        if (waitready && !numGenerated && !numLock)
        {
           numGenerated = true;
           numLock = false;
           finalCalled = false;

           int fV;
            //int buttonValue = Random.Range(minButtons, maxButtons);
           if (!finalCalled && !numLock)
            {
              generate();

              yield return new WaitForSeconds(1);
              fV = finalValue;
              yield return new WaitForSeconds(3);

              Debug.Log(fV); // ? why ?

              switch (finalValue){
                case 1:
                   waitready = false;
                   button2.GetComponentInChildren<Light>().color = green;
                   buttonfront2.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 2:
                   waitready = false;
                   button2.GetComponentInChildren<Light>().color = green;
                   buttonfront2.GetComponent<MeshRenderer>().material = greenLight;

                   break;
                case 3:
                   waitready = false;
                   button3.GetComponentInChildren<Light>().color = green;
                   buttonfront3.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 4:
                   waitready = false;
                   button4.GetComponentInChildren<Light>().color = green;
                   buttonfront4.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 5:
                   waitready = false;
                   button5.GetComponentInChildren<Light>().color = green;
                   buttonfront5.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 6:
                   waitready = false;
                   button6.GetComponentInChildren<Light>().color = green;
                   buttonfront6.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 7:
                   waitready = false;
                   button7.GetComponentInChildren<Light>().color = green;
                   buttonfront7.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 8:
                   waitready = false;
                   button8.GetComponentInChildren<Light>().color = green;
                   buttonfront8.GetComponent<MeshRenderer>().material = greenLight;
                   break;
                case 9:
                   waitready = false;
                   button9.GetComponentInChildren<Light>().color = green;
                   buttonfront9.GetComponent<MeshRenderer>().material = greenLight;
                   break;
               }
               isGenerated = false;
            }
        }
    }
}

I think adding yield break;我认为增加yield break; in each statement may fix your problem.在每个语句中都可以解决您的问题。

what i dont understant in your logic, you update temp variable inside method instead to update your variabel defined in class??我不明白你的逻辑,你更新方法内部的临时变量而不是更新你在 class 中定义的变量?

public class ButtonSystem : MonoBehaviour
{

        :
        :
    public int maxButtons = 9;
    public int minButtons = 1;

    int buttonValue;
    int finalValue;
    bool waitready;

    bool numLock;
    bool numGenerated;
    bool finalCalled;

    int[] buttons = new int[9] {1, 2, 3, 4, 5, 6, 7, 8, 9};

    IEnumerator waitbeforeStart()
    {
        yield return new WaitForSeconds(1);
        waitready = true;
        StartCoroutine(ButtonAlgo());
    }
IEnumerator ButtonAlgo()

Found the Answer Alright so I found the answer and the script I had in the first place was right.找到答案了,所以我找到了答案,而且我最初拥有的脚本是正确的。 In my project I had 9 buttons and the script was applied to each one of them meaning it was generating 9 different numbers which is was confused me I just found this out thanks for all the help everyone:)在我的项目中,我有 9 个按钮,脚本应用于每个按钮,这意味着它生成了 9 个不同的数字,这让我很困惑,我刚刚发现了这一点,感谢大家的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成一次 Random.Range 值,然后在 void Update 中使用它来移动? 团结,C# - How do I generate a Random.Range value once, and then use it in void Update to move? Unity, C# 在 C# 中生成随机大写字母,同时检查每个字母只生成一对 - Generation of random capital letters in C# while checking to generate only one pair from each 如何在c#中的给定范围之间生成随机数字序列 - How to generate random sequence of numbers between a given range in c# 如何在C#中使用十进制值生成随机数 - How to generate a Random number using Decimal value in C# 如何在MATLAB和C#中生成以相同值开头的随机数 - How to generate random number that start with the same value in MATLAB and C# 如何从C#列表中的记录中仅获取一个值? - How to get only one value from the record in the list in c#? 在 C# 中,只有在没有 class 属性的情况下,我才能生成一个值? - In C#, how can I generate a value for a class property only if there isn't one? 如何在C#中有效地生成从0到num的N个随机整数? - How to generate N random ints from 0 to num in C# efficiently? 如何从C#中的给定字符串生成随机字母数字字符串? - How to generate random alphanumeric string from a given string in C#? 生成不包括特定范围值的随机数 - C# - Generate Random Numbers excluding a certain range of values- C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM