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