Cant find NullReferenceException problem in following context

Hey Guys,

I’m trying to make a TowerDefense game and I have a problem when pressing my Upgrade() function, that currently is a Button with an OnClickEvent sitting on the UI of the selected turret.
I’m getting a NullReferenceException all the time and i just can’t figure out why.

Here is a Part of the upgrade function:

 public void UpgradeOne()
    {


        if (turret.StandardTurretBuilt == true)
        {

            turret.FirstUpgradeTurret();
            if (turret.isUpgradedOne == true)
            {
                Hide();
                StandardTurretLevelTwo.SetActive(true);
            }
        }

This calls the following function:

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

        Destroy(turret);
        Debug.Log("2");

        GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradeOnePrefab, mouseOver.GetBuildPosition(), Quaternion.identity);
        turret = _turret;
        Debug.Log("3");

        isUpgradedOne = true;
        isUpgradedTwo = false;
        isUpgradedThree = false;
        isUpgradedFour = false;
        Debug.Log("4");

        GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, mouseOver.GetBuildPosition(), Quaternion.identity);
        Destroy(effect, 5f);
        Debug.Log("5");

        Debug.Log("Turret upgraded");
    }

The Debug.Log()'s were just to find out where exactly the mistake was and it came out that the problem comes with the “turretBlueprint.upgradeOnePrefab”

Just to show you this is the “TurretBlueprint” script:

[System.Serializable]
public class TurretBlueprint {

    public GameObject prefab;
    public int cost;
    public int sellLevelOne;

    public GameObject upgradeOnePrefab;
    public int upgradeCostOne;
    public int sellLevelTwo;

    public GameObject upgradeTwoPrefab;
    public int upgradeCostTwo;
    public int sellLevelThree;

    public GameObject upgradeThreePrefab;
    public int upgradeCostThree;
    public int sellLevelFour;

    public GameObject upgradeFourPrefab;
    public int upgradeCostFour;
    public int sellLevelFive;
}

I had the upgrade function and all the necessary information sitting on the cube and the cubes script where the turret is built on and it worked. But I wanted to move it to the turret itself because it makes more sense in my opinion and basically i tried to mainly copy everything to a different script. Maybe while doing that I messed up with some GameObjects that I brought into the wrong context but I’m not quite sure. I tried to fix it for a long time now trying different things but there seems to be something I’m not seeing.
If there is any code missing you might need to find the problem feel free to ask for it.

Any answers appreciated

Can you post the exact error that’s showing up in the Console? That’ll show you the line number that the exception is happening on.

It’s hard to tell without knowing what line is throwing the error, but are you sure that all your variables are not null?

That’s the exact error. The line 255 it shows there is line 14 in the second code I posted:

NullReferenceException: Object reference not set to an instance of an object
Turret.FirstUpgradeTurret () (at Assets/Scripts/Turret.cs:255)
TurretUI.UpgradeOne () (at Assets/Scripts/TurretUI.cs:201)
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()

GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradeOnePrefab, mouseOver.GetBuildPosition(), Quaternion.identity);

One of those things is null. My guess is that it’s turrentBlueprint.upgradeOnePrefab. Try this before calling that:

if(turretBlueprint.upgradeOnePrfab == null)
{
    Debug.Log("PREFAB IS NULL");
}

It looks like this originated from a UnityEvent so if you’re setting up that prefab somewhere, this could I suppose be getting invoked before that happens.

Yeah that was my guess too but my problem is that I just cant see why it should be null. Normally NullReferences are one of the easier problems to fix but in this case I just can’t find the mistake. I do believe that I set all the references so in my opinion it should not be null but, as we can see, it is.
I will look over it a couple more times to maybe find it.
I just thought that there could be a scripting error.

What type of object is buildManager.buildEffect?
the Instantiate needs a GameObject, AKA a prefab

The buildEffect is a particlesystem. This doesn’t make any problems.

so it’s a prefab empty gamObject with a particle added to it?

I figured out that the problem was not that the turretBlueprint.upgradeOnePrefab was null, but the position vector. Thank you all for your help :slight_smile: