Search code examples
c#unity-game-engineonmousedown

OnMouseDown() null reference exception


public class TowerNode : MonoBehaviour {

public bool IsShopOpen=false;

//Z position
public Vector3 positionOffSet;

//colors
public Color hoverColor;
private Color startColor;

//GameObjects
public GameObject turret;

//shop
public Shop shop;

//Build Manager
BuildManager buildManager;

void Start()
{
    rend=GetComponent<Renderer>();
    startColor = rend.material.color;
    buildManager = BuildManager.instance;
    shop = GetComponent<Shop>();
}


//When mouse is on the turret node
public void OnMouseDown()
{    
    Debug.Log("Mouse is Down");
    bool IsShopOpen  = true;
    if (IsShopOpen == true)
    {
        Debug.Log("shop is open");
        shop.onEnable();
    }

    if (EventSystem.current.IsPointerOverGameObject())
    {          
        return;
    }
    if (!buildManager.CanBuild)
    {
        return;
    }
    if (turret != null)
    {
        Debug.Log("Cant Build Here!!!");
        return;
    }       

    buildManager.BuildTurretOn(this);

}

the other script of the shop is this:

public class Shop : MonoBehaviour
{
public TurretBlueprint Archery;
public TurretBlueprint Treb;
public TurretBlueprint Workamp;
public TurretBlueprint Barracks;

public Button Archeryy;
public Button Trebb;
public Button WorkCampp;
public Button Barrackss;

BuildManager buildManager;


void Start()
{
    buildManager = BuildManager.instance;
    disableAllButtons();
    //OnEnable();
}

public void SelectArchery()
{
    buildManager.SelectTurretToBuild(Archery);
    Debug.Log("archery!!!!");

}
public void SelectTreb()
{
    buildManager.SelectTurretToBuild(Treb);
    Debug.Log("Treb!!!!");
}
public void SelectWorkamp()
{
    buildManager.SelectTurretToBuild(Workamp);
    Debug.Log("Work Camp!!!!");
}
public void SelectBarracks()
{
    buildManager.SelectTurretToBuild(Barracks);
    Debug.Log("Barracks!!!!");
}


public void onEnable()
{
    Archeryy.gameObject.SetActive(true);
    Trebb.gameObject.SetActive(true);
    WorkCampp.gameObject.SetActive(true);
    Barrackss.gameObject.SetActive(true);

}

public void disableAllButtons()
{
    Archeryy.gameObject.SetActive(false);
    Trebb.gameObject.SetActive(false);
    WorkCampp.gameObject.SetActive(false);
    Barrackss.gameObject.SetActive(false);
}

}

I need when i click on the game object that the shop will open the sop got few buttons under it, but it gives me:

"NullReferenceException: Object reference not set to an instance of an object TowerNode.OnMouseDown () (at Assets/Scripts/TowerNode.cs:51) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)"

and I don't understand why.


Solution

  • The error you are getting implies that an object in the OnMouseDown function is null when executed:

    NullReferenceException: Object reference not set to an instance of an object TowerNode.OnMouseDown () (at Assets/Scripts/TowerNode.cs:51)

    You should look at ine 51 of TowerNode.cs and ascertain what can be null on that line. Could shop or buildManager be null? Perhaps this event is firing before you have initialised your TowerNode object.

    It seems you are trying to access a component (Shop) from a completely unrelated GameObject. If you want all of your TowerNode objects to reference the Shop then you could use GameObject.Find. This isn't really recommended though; it seems like there is a better way to link these two objects but I am unsure of your use case.