繁体   English   中英

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

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

从一个范围内只生成一个随机值/int我遇到了一个问题,当我从一个范围内生成一个随机值/int 时,它会生成 10,而我只希望它生成一个。 我已经尝试将生成随机 int 的代码放在 if 语句中,其中需要一个变量为 false 才能运行它,然后在 if 语句中将变量更改为 true,但这也不起作用。

random = Random.Range(0, 9);

上面的代码行是我用来生成随机整数的。 我还尝试了另一种方法,我将在下面展示。

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;

作为参考,这是我的整个脚本。

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;
        }       
    }
}

这是控制台的图像,它正在记录 finalValue 的值我希望它给出 1 个值,但它给出了几个

您错过的是:要选择一个随机索引,您应该从0开始。

所以将你的minButtonIndex1更改为0

注意:使用 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.
}

看:

您在使用它们之后设置minButtonsmaxButtons ,因此您应该在获取随机数之前设置它们。

看起来像这样:

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

啊,我找到了,我希望

创建一个 Void 以生成一个值,但它仅在 bool 不为 true 时才生成一个 // isGenerated = false 默认情况下

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

当您保护最终结果时,您可以将 isGenerated 设置为 false

这就是它最后的样子

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;
            }
        }
    }
}

我认为增加yield break; 在每个语句中都可以解决您的问题。

我不明白你的逻辑,你更新方法内部的临时变量而不是更新你在 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()

找到答案了,所以我找到了答案,而且我最初拥有的脚本是正确的。 在我的项目中,我有 9 个按钮,脚本应用于每个按钮,这意味着它生成了 9 个不同的数字,这让我很困惑,我刚刚发现了这一点,感谢大家的帮助:)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM