- Home /
Issue with GameObject has been destroyed but it is not destroyed
Hi there everyone. I am having a little issue with with a GameObject that is active in the scene but when the player has a collision on the GameObject, the GameObject will not disappear.
My Script is written to destroy the GameObject when a collision has occurred, however, the GameObject is not destroying.
I have included a photo of Unity's messages to let you know what is happening. You will see that the GameObject is throwing an error as it say "GameObject has been destroyed but you are still trying to call it." What is interesting is that after the fourth time of allowing the player to collide with the same GameObject as it respons, the player performs an upgrade function that is in the code attached to the player GameObject.
My question is: How do I go about and fix this issue as the GameObjects are working properly but still calling for an error as the GameObject has been destroyed when it is still currently in the scene.
Here is my code:
public enum BonusType1
{
levelup
}
public enum BonusType2
{
levelup
}
public class Bonus : MonoBehaviour {
public BonusType1 bonusType1;
public BonusType2 bonusType2;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
switch (bonusType1)
{
//if 'weapon power' not more than 'max weapon power', increase weapon power level
case BonusType1.levelup:
SoundManager.instance.PlaySound("powerUp");
if (PlayerShooting.instance.weaponPower1 < PlayerShooting.instance.maxweaponPower)
{
PlayerShooting.instance.weaponPower1++;
PlayerShooting.instance.guns1.PlayerRay.GetComponent<PlayerRay>().UpdateVfx();
}
break;
}
Destroy(gameObject);
}
if (collision.tag == "Player")
{
switch (bonusType2)
{
//if 'weapon power' not more than 'max weapon power', increase weapon power level
case BonusType2.levelup:
SoundManager.instance.PlaySound("powerUp");
if (PlayerShooting.instance.weaponPower2 < PlayerShooting.instance.maxweaponPower)
{
PlayerShooting.instance.weaponPower2++;
PlayerShooting.instance.guns2.PlayerRay.GetComponent<PlayerRay>().UpdateVfx();
}
break;
}
Destroy(gameObject);
}
}
}
Here is my pictures:
This is probably not the full answer for your question, but I think the 2 Destroy calls in your code might be causing problems. If the collision.tag does equal "Player" then it will enter both the first and second if-statements and therefore try destroy it twice, the second time it'll be destroying a gameobject that's been destroyed already (i.e. the exception descript)
Since both if-statements check the same thing, we can assume that if the predicate was true the first time then it'll also be true the second time - it'll either enter both or neither. So if you remove one of the Destroy calls, it'll still get destroyed (in theory, ignoring the next issue!) just as it would with both, but hopefully $$anonymous$$us the exceptions. If you can test that we can see whether that was causing the exceptions and if so, whether not having exceptions improves the behaviour.
Answer by $$anonymous$$ · Jan 22, 2021 at 12:49 AM
After a gameObject is destroyed, it will be set to null. When you try to access a gameObject that has been destroyed, it will throw an error and produce unpredictable results.
Change:
if (collision.tag == "Player")
to:
if (collision.tag == "Player" && gameObject != null)
The code works, however, it still does not explain why the player must hover over the GameObject 4 times before it will destroy it. There are 24 children applied to the player which has the sprites, Collider2D, and weapons. The first child works fine but when switching to any other child, the weapons upgrade will not increase until the player has hovered over the weapons upgrade GameObject 4 times.