Hi, I’m new in Unity and basically I followed a tutorial and it all worked but now suddenly I hit play and I’ve got a null exception error because Node script isn’t Starting.
Here I have a debug that isn’t showing in the console:
using UnityEngine;
using UnityEngine.EventSystems;
public class Node : MonoBehaviour
{
public Color hoverColor;
public Color notEnoughMoneyColor;
public Vector3 positionOffset;
[HideInInspector]
public GameObject turret;
[HideInInspector]
public TurretBlueprint turretBlueprint;
[HideInInspector]
public int isUpgraded = 0;
private Renderer rend;
private Color startColor;
BuildManager buildManager;
void Start()
{
Debug.Log("Nodo Start()");
rend = GetComponent<Renderer>();
if (rend != null)
{
startColor = rend.material.color;
}
else
{
Debug.LogError("Renderer not found on Node.");
}
buildManager = BuildManager.instance;
if (buildManager == null)
{
Debug.LogError("BuildManager instance not found in Node.");
}
}
Then I use the rend to change de colour of the node:
public void OnMouseEnter()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
if (buildManager == null)
{
Debug.LogError("BuildManager instance is null in OnMouseEnter.");
return;
}
if (!buildManager.CanBuild)
return;
if (buildManager.HasMoney)
{
rend.material.color = hoverColor;
}
else
{
rend.material.color = notEnoughMoneyColor;
}
}
public void OnMouseExit()
{
rend.material.color = startColor;
}
But when I hit play it says null reference and I don’t see the Debug in the Start in the console.
The error code when I pass the mouse over the nodes is:
NullReferenceException: Object reference not set to an instance of an object
Node.OnMouseExit () (at Assets/Scripts/Node.cs:188)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32)
The buildManager is called nice, I have an Debug in the Awake and it’s in the console when I test.
Anyone knows what’s happening?
Edit: The script was disabled in the inspector and that’s why the start wasn’t called.