On trigger enter void is calling twice
I am sorry of my English. My snake collides with two colliders at the same time and causes the same function twice and gets an error. According to my idea: when the snake eat the victim, then the tail add to the snake, but the problem arose when the snake eat two victims simultaneously and simultaneously the same function and it turns out that two tails are generated in one place. My script:
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("victim"))
{
AddTail();
}
}
public void AddTail()
{
Vector3 newTailPos = tailObjects [tailObjects.Count - 1].transform.position;
newTailPos.z -= z_offset;
tailObjects.Add (GameObject.Instantiate (TailPrefab, newTailPos, Quaternion.identity) as GameObject);
}
Comment
Your answer
Follow this Question
Related Questions
Obstacle appear when the player enter in a Trigger 1 Answer
How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer
Weird text field bug? 1 Answer
Object reference not set to an instance of an object 0 Answers
How to move a game object to a position after selecting it 0 Answers