- Home /
Spawn object from Random Vector3 in array
I have an array set up that has 12 different locations. I have another script set up that will spawn the prefab in after a certain number of seconds. When I spawn that object, I want it to spawn at one of the location in my array. How can I pick a random Vector3 from my array, and set the objects position to that Vector3? Thanks :)
P.S. This is slightly urgent, considering this is the last thing I need for my current 3 hour game jam that ends in 30 mins. Thanks again! :)
transform.position = spawnPoints[Random.Range(0,spawnPoints.Length)];
Wouldn't it need to be spawnPoints.Length -1 as arrays start at 0
$$anonymous$$g if we have 10 vectors in array. .Length would return 10 but the final vector would be at index number 9 so if the random number generates 10 it could cause an error.
???
no, robertbu is correct
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
for int : Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive]