- Home /
Instantiating random prefabs
Hello, We are trying to make a random pickup appear in a random place. i think i have the sort of right lines but it keeps giving us an error message so it's definatly not right lol. This is what we have so far.
var prefabOnLevel : boolean = false; var nextSpawnTime : float = 2.0; var timeBetweenSpawns : float = 30.0;
function Awake() { var SpawnWhat : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUp"); var SpawnThis : GameObject = SpawnWhat[Random.Range(0,SpawnWhat.length)]; var PickUpSpawn : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUpSpawn"); var SpawnHere : Transform = PickUpSpawn[Random.Range(0, PickUpSpawn.length)].transform; }
function Update () { if (!GameObject.FindWithTag ("PickUp")) { prefabOnLevel = false; }
if (nextSpawnTime < Time.time)
{
nextSpawnTime = Time.time + timeBetweenSpawns;
if (prefabOnLevel == false)
{
Instantiate (SpawnThis, SpawnHere.position, SpawnHere.rotation);
Debug.Log ("should be a pick up in level");
}
}
}
This particular format gives us the error of Unknown variable on the Instantiate line.
function Update () { if (!GameObject.FindWithTag ("PickUp")) { prefabOnLevel = false; }
if (nextSpawnTime < Time.time)
{
nextSpawnTime = Time.time + timeBetweenSpawns;
if (prefabOnLevel == false)
{
var SpawnWhat : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUp");
var SpawnThis : GameObject = SpawnWhat[Random.Range(0,SpawnWhat.length)];
var PickUpSpawn : GameObject[] = GameObject.FindGameObjectsWithTag ("PickUpSpawn");
var SpawnHere : Transform = PickUpSpawn[Random.Range(0, PickUpSpawn.length)].transform;
Instantiate (SpawnThis, SpawnHere.position, SpawnHere.rotation);
Debug.Log ("should be a pick up in level");
}
}
}
this format gives us the error of "Array index is out of range" at runtime, in the line var SpawnThis. What are we doing wrong?
Answer by DaveA · Feb 28, 2011 at 11:25 PM
In the first, your variables are local to Awake, you'd need to put them outside.
In the second, Random.Range is inclusive, so if the length were, say, 5, it might return 5, which would be out of bounds. Try Random.Range(0,PickUpSpawn.length-1)
Iv tried the second option with Random.Range(0,PickUpSpawn.length-1) and it still brings back "Array index is out of range" at runtime. Thanks for your response, could you offer any more insight into the problem? Is there any more information I can provide you with?
In that case, I'd hit the debugger and/or put in some Debug.Log to see that all things are what I expect. I suspect the 'Find' in the preceding line is not finding anything.
Also, if you are creating those pickups with a script, it would be more efficient to keep track of those (in a list or array), rather than use Find because finding is slow.
lol nice :D Bob, also make sure you're changing both your random ranges to be -1 to the length, or you'll still get the out of range for your SpawnHere
Your answer
Follow this Question
Related Questions
IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers
Class Array referencing 1 Answer
Can't Re-Enable Lightmaps after Disabling Them at Runtime 1 Answer
Find the average of 10 Vectors 4 Answers