NullReferenceException: Object reference not set to an instance of an object

I can link the other script it’s referencing if needed.

using System;
using UnityEngine;

public class Node : MonoBehaviour
{

    public Color hoverColor;
    public Vector3 positionOffset;

    private GameObject turret;
    private Renderer rend;
    private Color startColor;

    void Start()
    {
        rend = GetComponent<Renderer>();
        startColor = rend.material.color;
    }

    void OnMouseDown ()
    {
        if (turret != null)
        {
            Debug.Log("Can't build there! - TODO: Display on screen.");
            return;
        }

//---->        GameObject turretToBuild = BuildManager.instance.GetTurretToBuild();
        turret = (GameObject)Instantiate(turretToBuild, transform.position + positionOffset, transform.rotation);

    }


    void OnMouseEnter()
    {
        rend.material.color = hoverColor;
    }

    void OnMouseExit()
    {
        rend.material.color = startColor;
    }

}

Heres the reference script.

using UnityEngine;

public class BuildManager : MonoBehaviour
{
    public static BuildManager instance;

    void awake()
    {
        if (instance != null)
        {
            Debug.LogError("More than one BuildManager in scene!");
            return;
        }

        instance = this;

    }

    public GameObject LaserTower;

    void Start()
    {
        turretToBuild = LaserTower;
    }

    private GameObject turretToBuild;

    public GameObject GetTurretToBuild()
    {
        return turretToBuild;
    }
}

I just noticed, is “void awake()” a typo or could it be problem?

Did you assign the game objects to the inspector? In your BuildManager, you refer to a turret game object, did you drag and drop it in?

@NoctisShadowzel I’m not entirely sure, I’m still fairly new to programming. awake is invoked before start gets invoked. That’s why I put it there.

@Volcanicus I did, multiple times.

Thank you, I didn’t notice that “awake()” wasn’t capitalized. THAT was my issue. I’ve looked over these 2 scripts over and over and didn’t see that.

No need to thanks, it happens… I wish you success!

1 Like

Are you using VisualStudio2019 or comparable IDE? They should give you a warning message when methods are not capitalized.