Problem in Destroy GameObject on OnTriggerEnter2D
I just learning from this tutorial Unity Tutorial - Make 2D Flappy Bird
What I want : Destroying "trash" object when "swimmer" trigger it.
Error : The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. TrashPool.Update ().
What I have done :
Trash.cs
private void OnTriggerEnter2D (Collider2D other){
if (other.GetComponent<Swimmer> () != null) {
GameControl.instance.SwimmerScored ();
Destroy (gameObject);
}
}
TrashPool.cs
void Update() {
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
//Set a random y position for the column
float spawnYPosition = Random.Range(columnMin, columnMax);
//...then set the current column to that position.
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
//Increase the value of currentColumn. If the new size is too big, set it back to zero
currentColumn ++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
I hope someone can help me to resolve this problem, Thank you.
Your answer
Follow this Question
Related Questions
Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers
Destroying GameObject after amount of triggers 2 Answers
Destroying Object OnTriggerEnter2D not working 1 Answer
How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer
Weird text field bug? 1 Answer