Need help removing a tail prefab on collision.

Hi I am coding a tron type game and I am trying to get my tail to reset/delete on collision. I don’t want to use Application.LoadLevel(Application.loadedLevel); because it resets both players. Anyone know how to delete the list of tails on collision from a prefab? If so could you send me the code .

Some questions:
How are you currently creating the tails?
Are they separate objects with colliders?
What holds the list of them?

Can you do a foreach loop on your list and use Destroy()?

How are you currently creating the tails?
List Tail = new List();
public GameObject TailPrefab;

void Move ()
Vector2 v = transform.position;

transform.Translate(dir);
GameObject g = (GameObject)Instantiate(TailPrefab, v, Quaternion.identity);

Tail.Insert(0, g.transform);

Are they separate objects with colliders?
Yes they are seperate objects witha 2d box collider.

What holds the list of them?
Should be answered in the first question.

Can you do a foreach loop on your list and use Destroy()?
Don’t understand what a foreach loop is though I have tried using Destroy() before though I didn’t work.

When you detect a collision, try this type of approach using a loop:

// destroy the tail
foreach(Transform tailSegment in Tail){
    Destroy(tailSegment.gameObject);
}
// empty the list of null values after the gameObjects are destroyed
Tail.Clear();

Thank You soo much that works perfectly

1 Like