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.
Thank you, Gerald.