How can I instantiate two prefabs at different times and positions ?

Hi everyone,

I am a newbie in unity and was trying to instantiate two prefabs on 3 different positions and different times too. I want these prefabs two instantiate with a minimum of 1.5 seconds and max of 3 seconds. They should not interefere with each other. I can instantiate one prefab randomly chosing the position and time between 1.5 and 3 seconds and it works well for me. The problem is when I instantiate the second prefab because it intereferes with the first object. Both of these prefabs have a method where they can pick up randomly the position 0, 1, or 2 and the time from min to max.

public Transform[] posTranform;
public RigidBody[] prefab;

private RigidBody prefabInstance1;
private RigidBody prefabInstance2;

private float timer1;
private float timer2;

int randTransform1;
int randTransform2;

private float minTime = 1.5f;
private float maxTime = 3.0f;

void SetRandomPosition1()
	{
		randTransform1 = Random.Range (0, 3);
	}

void SetRandomPosition2()
	{
		randTransform1 = Random.Range (0, 3);
	}

void SetRandomTime1()
	{
		spawnTime1 = Random.Range (minTime, maxTime);
	}

void SetRandomTime2()
	{
		spawnTime2 = Random.Range (minTime, maxTime);
	}

The above methods are called inside of these two conditions set on Update()

void Update ()
{
timer1+=Time.deltaTime;
timer2+=Time.deltaTime; 

if (timer1 >= spawnTime1) { 
timer1 =0;
prefabInstance1 = Instantiate(prefab[0], posTransform[randomTransform1].position, posTransform[randomTransform1].rotation) as RigidBody;
SetRandomTimer1(); SetRandomPosition1();
} 
 if (timer2 >= spawnTime2) {
timer2 = 0;
prefabInstance2 = Instantiate(prefab[1], posTransform[randomTransform2].position, posTransform[randomTransform2].rotation) as RigidBody;
SetRandomTimer2(); SetRandomPosition2();
}
}

If someone knows how to make the prefabs not interefere with each other it could be great. :wink:

Thank you, Gerald.

Ok, I kindof see what you mean from your last comments, kind off… :slight_smile:

You really should not be using Update. Really… You do not need new random numbers at every frame as you are saying, you just need them just before instantiating your prefabs.

Do you know about Enumerators? if not, read on those, they will help you doing what you want with much better performance than Update. I was doing same than you before, and that messes up the code and complicates thing. Use Enumerators :slight_smile: .

Here is an untested solution, but that should work for you, if I’m correct, this should replace all your original code and do what you want. And be cleaner…

public Transform[] posTranform;
public RigidBody[] prefab;

private int randTransform1;
private int randTransform2;

private float minTime = 1.5f;
private float maxTime = 3.0f;

void Start()
{
	StartCoroutine(Spawn1(Random.Range(minTime,maxTime)));
	StartCoroutine(Spawn2(Random.Range(minTime,maxTime)));
}

private IEnumerator Spawn1(float waitTime)
{
	//this will force the function to pause for 'waitTime' time
	yield return new WaitForSeconds(waitTime);
	
	while (randTransform1 == randTransform2)
	{
		//this will force randTransform1 to be different than randTransform2 and thus not interfere with the other
		randTransform1 = Random.Range(0,3);
	}
	
	Instantiate(prefab[0], posTransform[randTransform1].position, posTransform[randTransform1].rotation) as RigidBody;
	StartCoroutine(Spawn1(Random.Range(minTime,maxTime)));
}

private IEnumerator Spawn2(float waitTime)
{
	yield return new WaitForSeconds(waitTime);
	
	while (randTransform1 == randTransform2)
	{
		randTransform2 = Random.Range(0,3);
	}
	
	Instantiate(prefab[1], posTransform[randTransform2].position, posTransform[randTransform2].rotation) as RigidBody;
	StartCoroutine(Spawn2(Random.Range(minTime,maxTime)));
}