- Home /
Array of prefabs with different sizes stick to each other
Hello, Im quite new to Unity and coding. (really like it though) :) I'm struggling with a prefab array that I instantiate and move. See the code below. I attached a separate script to the prefabs with a x.movement like this transform.Translate(Vector3.right
Now the prefabs are instantiated on a timer and they move in the x position. But this is not what I want. The prefabs are different sizes in the x. I want my instantiate to know what the sizes are and instantiate them with a given gap in between. I did a lot of googling and came across bound.size.x and renderer.bound. I think I need to calculate the timer to a prefab bound size x, but don't know how... Also I think I need the movement script in this timer, not as a separate script. Am I on the right track? Can anyone give me some clues?
Thanks!
private float timer = 0.0f;
private float TimeToRespawn = 2.0f;
public Transform[] Spawns;
public GameObject[] Prefabs;
void FixedUpdate () {
timer += Time.fixedDeltaTime;
if (timer >= TimeToRespawn)
SpawnPFB();
}
void SpawnPFB(){
timer = 0;
int tele_num = Random.Range(0,1);
int prefeb_num = Random.Range(0,2);
Instantiate(Prefabs[prefeb_num],Spawns[tele_num].position, Spawns[tele_num].rotation);
}
}