Search code examples
c#unity-game-enginenullreferenceexceptiononmousedown

Unity cant find the other script NullReferenceException: Object reference not set to an instance of an object


So my TowerNode script cant find the Shop script and active or disable it for some reason it just cant find it so it give me a "NullReferenceException: Object reference not set to an instance of an object TowerNode.OnMouseDown () (at Assets/Scripts/TowerNode.cs:46)"

its cant make the Shop.Enable method on line 46

this is the TowerNode script

public bool IsShopOpen = false;
//Z position
public Vector3 positionOffSet;

//colors
public Color hoverColor;
private Color startColor;

//GameObjects
public GameObject turret;

//shop
public Shop shop;


//renderer!!
private Renderer rend;

//Build Manager
BuildManager buildManager;

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

    shop = GetComponent<Shop>();
    GameObject.Find("Shop");


}
//When mouse is on the turret node
void OnMouseDown()
{

    bool IsShopOpen = true;
     if (IsShopOpen == true)
    {

        shop.Enable();
    }


    if (EventSystem.current.IsPointerOverGameObject())
    {


        return;
    }
    if (!buildManager.CanBuild)
    {
        return;
    }
    if (turret != null)
    {

        Debug.Log("Cant Build Here!!!");
        return;
    }

    buildManager.BuildTurretOn(this);

}

public Vector3 GetBuildPosition()
{
    return transform.position + positionOffSet;
}

//when mouse get into the node space
void OnMouseEnter()
{
    rend.material.color = hoverColor;
    if (EventSystem.current.IsPointerOverGameObject())
    {
        return;
    }
    if (!buildManager.CanBuild)
    {
        return;
    }

}
//when mouse exit the node space
void OnMouseExit()
{
    rend.material.color = startColor;
}}

And this is the Shop script:

using UnityEngine; using UnityEngine.UI;

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();
    //Enable();
}

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 Enable()
{

    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);
}}

Solution

  • shop = GetComponent<Shop>();
        GameObject.Find("Shop");
    

    I'm assuming the Shop script resides on the GameObject named Shop, and not the TowerNode GameObject, which would mean you should get the Shop script from that gameobject like so:

    shop = GameObject.Find("Shop").GetComponent<Shop>();