MissingReferenceException cant find out why

Hey guys,

I’m trying to upgrade towers in a towerdefense game and I get the following error when trying to run the following code:

   public void FirstUpgradeTurret()
    {

        if (PlayerStats.Money < turretBlueprint.upgradeCostOne)
        {
            Debug.Log("Not enought money to upgrade!");
            return;
        }
        PlayerStats.Money -= turretBlueprint.upgradeCostOne;

        Destroy(turret);
        GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradeOnePrefab, GetTurretPosition(), Quaternion.identity);
        turret = _turret;

        isUpgradedOne = true;
        isUpgradedTwo = false;
        isUpgradedThree = false;
        isUpgradedFour = false;

        GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetTurretPosition(), Quaternion.identity);
        Destroy(effect, 5f);

        Debug.Log("Turret upgraded");
    }

    public void SecondUpgradeTurret()
    {
        if (PlayerStats.Money < turretBlueprint.upgradeCostTwo)
        {
            Debug.Log("Not enought money to upgrade!");
            return;
        }

        PlayerStats.Money -= turretBlueprint.upgradeCostTwo;

        Destroy(turret);

        GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradeTwoPrefab, GetTurretPosition(), Quaternion.identity);
        turret = _turret;

        isUpgradedOne = false;
        isUpgradedTwo = true;
        isUpgradedThree = false;
        isUpgradedFour = false;

        GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetTurretPosition(), Quaternion.identity);
        Destroy(effect, 5f);
        Debug.Log("Turret upgraded");
    }

MissingReferenceException: The object of type ‘Turret’ 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.
Turret.GetTurretPosition () (at Assets/Scripts/Turret.cs:347)
Turret.SecondUpgradeTurret () (at Assets/Scripts/Turret.cs:283)
TurretUI.UpgradeTwo () (at Assets/Scripts/TurretUI.cs:248)
UnityEngine.Events.InvokableCall.Invoke (System.Object[ ] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

I just posted a part of the script, there are more upgrades but i think it is enough looking at this part. It says that the reference is missing and I don’t really know why. Every upgrade i set my turret as the new instantiated turret but when i deleted the Destroy(turret) on the first upgrade then it worked so that tells me that no matter what upgrade the reference is always the first turret and this is what I don’t understand because I’m updating the turret everytime with the “turret = _turret;”

If anyone knows what I did wrong I would be happy to know that :slight_smile:

The exception is being thrown in Turret.GetTurretPosition, so you should post that too.

But I guess GetTurretPosition() is using turret in some way? And since you destroy that right before, you get MissingReferencesException.

Thank you for your answer.

The GetTurretPosition is simply this one:

    public Vector3 GetTurretPosition()
    {
        return transform.position;
    }

I just tried the same thing but with Vector3.up just to have a position that is independent from anything and it worked so thank you for that so far.

Is there a way I can use this GetTurretPosition() to get the position of the turret that gets replaced? Or any other method of getting this position.

I tried a bit more and when i do the FirstUpgradeTurret() then everything works even if i destroy the GameObject before getting the position for some reason. It just doesn’t work on the further upgrades. It seems that the reference is only applied on the first turret because when I don’t destroy the first turret then i can do the further upgrades aswell. It only makes problems when the first turret gets destroyed because thats the one the script references but why doesn’t that update?

Did you try what @Havokki said and made the Instantiate before doing the Destroy?

Yes I tried that but that doesn’t work either

You need to understand your problem so that you can go and study your code. What is happening is that you are trying to access a Turret type reference that has already been destroyed or marked for destroy. You will need to read your code and follow it so that you can see why that might be happening.