- Home /
How do i prevent an object instantiating another object straight away
my c# code below instantiates an array of gameobjects in a range of specific time. It all works fine though except I have a simple problem. In my game I have powerups and the spawner object that is instantiating the powerups instantiates it right where the spawner object is for the first time. so if my spawnMin was 7 and spawnMax was 20 it would rarely instantiate the object which it does but it also instantiates it once as soon as the script is called and it gets spawned right on the position of the object that is spawning it but only once? How do i prevent this?
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2;
void Start () {
Spawn();
}
void Spawn()
{
Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range (spawnMin, spawnMax));
}
To the OP: Random.range (on int) is exclusive for $$anonymous$$AX, so in your code example of $$anonymous$$/max 1-2, 1 is the only possible result. Also look at InvokeRepeating.
Answer by HYPERSAVV · Jan 04, 2015 at 06:45 AM
A little confused by the wording of your question. What I got is that you have a spawner object, and I assume it is moving around in your game, and randomly you'd like it to spawn power-ups (which we want) but it also always spawns one right away (which we don't want).
Assuming that, your problem is that your first call to Spawn (in Start) will always be instantly when the script is enabled. Once you've called Spawn it's fine, because within Spawn we are Invoking Spawn, again, with a random time value. Another way to do this would be:
void Start()
{
Invoke("Spawn", Random.Range( spawnMin, spawnMax));
}
void Spawn()
{
Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range (spawnMin, spawnMax));
}
tl;dr You need to treat the first call to Spawn like each successive call to it.
Answer by aaronpaulina · Jan 04, 2015 at 04:33 PM
You are calling Spawn() right away in start. That will call the function as soon as the object is loaded. Also by setting it's position to transform.position you are using the position of the spawner as your position for the powerup.
Change your Start method to something like this..:
void Start()
{
InvokeRepeating("Spawn",2f,Random.Range(min,max));
}
and your Spawn to :
void Spawn()
{
GameObject newobj = (GameObject)Instantiate(obj[Random.Range(0,obj.Length]));
newobj.transform.Position = new Vector3(Random.Range(minx,maxx),Random.Range(miny,maxy),Random.Range(minz,maxz));
}
While InvokeRepeating would fix the first issue, although not entirely since the first invocation is a fixed time, it would introduce a new issue with lack of randomization. Each successive call to Spawn would happen at a constant interval.
Your answer
Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
Random select from array and spawn 1 Answer
Multiple random spawns from an array 1 Answer
Spawning Objects Using An Array. 1 Answer
Spawn from array 2 Answers