"variable can't used before it's declared"...but I'm not allowed to declare it either!

I’m trying to get a list of 10 random numbers where no number repeats itself twice.

The errors I get are:

Assets/Scripts/Multiplex.cs(30,34): error CS0841: A local variable rand' cannot be used before it is declared Assets/Scripts/Multiplex.cs(30,29): error CS1502: The best overloaded method match for System.Collections.Generic.List.Add(int)’ has some invalid arguments
Assets/Scripts/Multiplex.cs(30,29): error CS1503: Argument #1' cannot convert object’ expression to type `int’

If I try to declare rand as an “int” I get another error that it “conflicts with a child block”…anyway, here’s the script… I will appreciate help!

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


//Get a list a 10 random numbers, but no number can repeat itself twice

public class Multiplex : MonoBehaviour {
    private bool NewNumber;
    private int counter;
    List<int> randnumbers = new List<int>();
    private int listrand;
   
    void Start () {
        NewNumber = false;
        counter = 0;
        while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
            int rand = Random.Range(0, 10); //generates a random number from 0-10
            foreach (int listrand in randnumbers) {
                if (listrand != rand)
                {
                    counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
                }
            }
                if (counter < randnumbers.Count)
                        {
                            NewNumber = true; //only if there is single mismatch should this be true
                        }
                            }
        randnumbers.Add (rand); //the new number is added to the list
            }
}

Move rand out of the while loop

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//Get a list a 10 random numbers, but no number can repeat itself twice
public class Multiplex : MonoBehaviour {
    private bool NewNumber;
    private int counter;
    List<int> randnumbers = new List<int>();
    private int listrand;
  
    void Start () {
        NewNumber = false;
        counter = 0;
       int rand = Random.Range(0, 10);
        while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
            rand = Random.Range(0, 10); //generates a random number from 0-10
            foreach (int listrand in randnumbers) {
                if (listrand != rand)
                {
                    counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
                }
            }
                if (counter < randnumbers.Count)
                        {
                            NewNumber = true; //only if there is single mismatch should this be true
                        }
                            }
        randnumbers.Add (rand); //the new number is added to the list
            }
}

Thank you. Now I have another question if you will… Why doesn’t Debug.Log print anything on the screen when I do Debug.Log (listrand) ?

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


//Get a list a 10 random numbers, but no number can repeat itself twice

public class Multiplex : MonoBehaviour {
    private bool NewNumber;
    private int counter;
    List<int> randnumbers = new List<int>();
    private int listrand;

   
    void Start () {
        for (int i=1; i==10; i++) {
            NewNumber = false;
            counter = 0;
            int rand = Random.Range (1, 10); //generates a random number from 0-10
            while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
                rand = Random.Range (1, 10); //generates a random number from 0-10
                foreach (int listrand in randnumbers) {
                    Debug.Log (listrand); //WHY DOESN'T IT PRINT ANYTHING?
                    if (listrand != rand) {
                        counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
                    }
                }

                if (counter < randnumbers.Count) {

                    NewNumber = true; //only if there is single mismatch should this be true
                }
            }
            randnumbers.Add (rand); //the new number is added to the list
            //Debug.Log (counter);
        }
        /*if (randnumbers.Count == 9) {
            foreach (int nums in randnumbers) {
                Debug.Log (nums);
            }
        }*/
            }

}

does it give you error? because you already defined listrand. in debug.log set any variable name that is not defined like

foreach (int potato in random...)

Debug.Log(potato);

On the screen?..you probably mean in the console right?

On the right, you can also see some filters, maybe one of those is checked, so Log doesn’t show.

I meant on the console, yes. None of those filters are checked (if you’re refering to “Clear” “Collapse” “Clear on Play” “Error Pause”)…

does it give you error? because you already defined listrand. in debug.log set any variable name that is not defined like

Even if I remove its definition before the void Start it doesn’t appear to matter

I’m getting a warning that
Assets/Scripts/Multiplex.cs(9,22): warning CS0414: The private field `Multiplex.NewNumber’ is assigned but its value is never used

try with for loop

for (int i = 0, i < randnumbers.Count; i++){
   print(randnumbers[i]);
   if (randnumbers[i] != rand){
      counter++;
}
}

EDIT:randnumbers.Add(rand); //this part should go before your loop. basicly your list is empty.

If I’ll put it before the loop I won’t get 10 unique random numbers, I’ll get lots of repeats

What you do here is that you check empty list.

i would go like this

do{
   int tempint = RandomNumber();
}while(tempint == 100);
list.Add(tempint);

int RandomNumber(){
int randomnumber = Random.Range(1,10);
for (int i = 0; i < list.Count; i++){
   if (randomnubmer == list[i]{
     return 100;
   }
   else
      return randomnumber;
  }
}

this ensures that your number is unique

Well I eventually solved in by another method…literally used another method :slight_smile: The “contain” one… makes much more sense than relooping.

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


//Get a list a 10 random numbers, but no number can repeat itself twice

public class Multiplex : MonoBehaviour {
    private bool NewNumber;
    private int counter;
    List<int> randnumbers = new List<int>();

   
    void Start () {
        while (randnumbers.Count < 10) {
                int rand = Random.Range (1, 11); //generates a random number from 1-10
                if (randnumbers.Contains (rand) == false) {
                    randnumbers.Add (rand); //the new number is added to the list
                }
            }
        foreach (int listrands in randnumbers) {
            Debug.Log (listrands);
        }
    }
}

Thank you all for the feedback!