- Home /
Question by
Mrniceplant · Nov 25, 2019 at 08:07 PM ·
destroyrespawnmultiple objects
How can I Destroy and respawn random Objects from object List?,
I have made a game that it has 3 different type of objects and i destroy them when they collide with a Stick and then respawn one of the 3 objects again randomly.The problem is that when one object gets destroyed it is also removed from the list.
public Transform spawnpoint;
public GameObject[] objects;
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "destroyer")
{
Instantiate(objects[UnityEngine.Random.Range(0, objects.Length - 1)], spawnpoint.position, Quaternion.identity);
Destroy(this.gameObject);
}
},
Comment
Answer by prof · Nov 26, 2019 at 05:17 AM
You can disable/hide object instead of destroying it. Also use separate lists, one for prefabs and one for instantiated objects.
https://catlikecoding.com/unity/tutorials/object-management/