- Home /
spawn objects in random locations, check if location already taken
Hi! you'll see, i have this scripts that i made, i have 8 letters i wanna spawn in 11 possible spots, i want each letter to be assign to one unique location, but when i run the script, some letters are spawned at the same spot. What am i doing wrong? :I Thnks! :b
var letter : GameObject[]; //prefabs that will be spawned
var letterSpot : GameObject[]; //Empty GameObjects where the prefabs will spawn
var spotUsed : boolean[]; //wether the spot is already used or not
private var letterIndex : int;
function Start ()
{
PutLetter(0);
PutLetter(1);
PutLetter(2);
PutLetter(3);
PutLetter(4);
PutLetter(5);
PutLetter(6);
PutLetter(7);
}
function PutLetter (i : int)
{
letterIndex = Random.Range(0,10);
while (spotUsed == true)
{
letterIndex = Random.Range(0,10);
}
Instantiate(letter[i], letterSpot[letterIndex].transform.position, letterSpot[letterIndex].transform.rotation);
spotUsed[letterIndex]=true;
}
Answer by Maui-M · Aug 17, 2015 at 05:28 PM
You have to check that specific index in spotUsed to see if it is true. You might also have to initialize the spotUsed array to have a value of false.
while (spotUsed == true)
should be:
while (spotUsed[letterIndex] == true)
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
MoveTowards not moving towards! lol. 1 Answer
Spawn Sytem and collider-Trigger to disabling / re-enabling same Collider in a While 1 Answer
"MissingMethodException: Method not found:" (Javascript) 1 Answer