Home > database >  How to generate only one random value from a range c#
How to generate only one random value from a range c#

Time:11-22

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. 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.

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

CodePudding user response:

what you missed is that : to choose a random index you should start from 0.

so change your minButtonIndex from 1 to 0.

NOTE : use switch instead of if else statement like :

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

CodePudding user response:

Look :

you are setting the minButtons and maxButtons after you used them so you should set those before getting the random number.

Look like this :

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

CodePudding user response:

I think adding yield break; in each statement may fix your problem.

CodePudding user response:

what i dont understant in your logic, you update temp variable inside method instead to update your variabel defined in 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()

CodePudding user response:

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 generate(){
    if(isGenerated == true){
        // generate one
        isGenerated = true;
    }
}

and when you safed the final result you can set isGenerated to 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;
            }
        }
    }
}

CodePudding user response:

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 :)

  • Related