- Home /
Array choosing more than one random choice in Start function
I have an 'array' made in the start function, and a Random.Range to select an game object within the array (also in the start function).
The code is attached to 9 Game Objects.
When the code runs, the console prints out several (around 65) choices made by the 9 Random.Range's.
As the code is in function Start, i thought it would only run once per object...so why is it making 65 choices from 9 calls?
function Start ()
{
var buildingsLeft : GameObject[]; //sets up the array
buildingsLeft = GameObject.FindGameObjectsWithTag("building"); //populates the array
var selected : int = Random.Range(0, buildingsLeft.length); //randomly chooses an item from the array
print ("selected = " + selected);
}
Thanks for your brain-knowledge, Tom :)
Sounds like you have multiple objects with that script attached to them. :P
:)
I actually do...BUT! When it's attached to say, 9 gameobjects, it randomly selects around 65 random objects from the array, rather than the 9 it should be!
Figure out the culprit!
function Start ()
{
Debug.Log(gameObject.name);
Haha thanks. Check to make sure you have "clear on play" selected in your console. This has screwed me up many times before. Also, "Collapse" has been a menace to me as well.