Loop to populate array crashes Unity

I am trying to write a loop that populates my array “tileNumberArray” with unique, random numbers. I am aware that there are already quite a few solutions posted on Unity Answers that involve shuffling, etc. I may have to revisit those ideas, but I currently can’t get any of them to work because the syntax I’ve found has so far been unrecognized by Unity.

What I have pasted below almost works. It:

  1. Generates a random number
  2. Checks to see if that random number is already in tileNumberArray
  3. If so, it calls the “pickNewNumber” function, which will loop until it finds a number that isn’t used yet.

When it finds an unused number, it exits the while loop without assigning the “randomNumber” to the current index in “tileNumberArray”.

The assignment statement:

tileNumberArray = randomNumber;
HAS to be within that else clause at the end of my Start() function. It seems that if I put it anywhere else, Unity crashes. If it has to generate a new random number, I want it to assign the number before exiting the while loop and have it continue and finish populating my array. But this way, it just skips assigning if it runs into duplicates.
I’m probably missing something obvious… And if this solution is too inefficient or convoluted, I will try to get other methods to work.
Thank you in advance!
//Array to hold randomly-generated numbers
var tileNumberArray = new Array(4);

//Initialize the loop variable
var i = 0;

var randomNumber = 0;

var numberAlreadyExists = false;

function Start () {

//Populate the tileNumber array with numbers.
for (i = 0; i < tileNumberArray.length; i++){

//Generate a random number between 1 and 4
randomNumber = Random.Range(1, 4);

  • //If this number already exists in the array, generate another random number*

  • if (randomNumber in tileNumberArray){*

  • Debug.Log(“Random Number " + randomNumber + " is already in the array”);*

  • numberAlreadyExists = true;*

  • pickNewNumber();*

  • Debug.Log("New number is: " + randomNumber);*

  • }*

  • else{*

_ tileNumberArray = randomNumber;_

_ Debug.Log("Tile " + i + " is " + tileNumberArray*);


function pickNewNumber(){

while (numberAlreadyExists == true){

* Debug.Log(“Finding unused number…”);*

* //Generate a random number between 1 and 4*
* randomNumber = Random.Range(1, 4);*

* if (randomNumber in tileNumberArray){*

* Debug.Log(“New Random Number " + randomNumber + " is already in the array.”);*
* }*

* else {*

* Debug.Log(“New Random Number " + randomNumber + " is NOT in the array”);*

* numberAlreadyExists = false;*
* }*
* }*

* }*

Some things:

  1. Never use the Array class; it’s slow, untyped, and will cause problems. Use built-in arrays or a generic 1.

  2. Only declare variables inside functions, unless they absolutely must be shared between functions.

And the big one:

  1. Random.Range (1, 4) doesn’t pick a number between 1 and 4. See the docs; Range with ints is exclusive, so it only picks between 1 and 3.

You’d be better off using a shuffle bag algorithm rather than using while loops like this.