- Home /
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[i] = 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[i] = randomNumber;
Debug.Log("Tile " + i + " is " + tileNumberArray[i]);
}
}
}
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;
}
}
}
Because my question is probably overly confusing, here is an example from my console after running the above script.
Answer by Eric5h5 · Jul 27, 2014 at 05:59 PM
Some things:
1) Never use the Array class; it's slow, untyped, and will cause problems. Use built-in arrays or a generic List.
2) Only declare variables inside functions, unless they absolutely must be shared between functions.
And the big one:
3) 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.
Thank you for your quick and helpful response! Yeah I'm definitely not an expert at efficient program$$anonymous$$g (or ... in general), as you can see, haha. And I was noticing that I was never seeing 4 with my range results but was putting off looking into that. I will look into your suggestions. Thank you again!
Your answer
Follow this Question
Related Questions
Unity crashes when doing A* pathfinding loop 1 Answer
Infinite Looping Crash 2 Answers
Unity not picking up errors until after reopening 0 Answers
Infinite loop crash System.Enum.ToString() 0 Answers
Unity freezes when OnTriggerEnter occurs 3 Answers