-1

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);
}}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Lian Levi
  • 33
  • 9
  • Please don't tag your question is unnecessary tags. You are not using [unityscript], you are using [C#]. From the tag wiki: *`Do not use this tag to describe Unity scripts which are in C#: UnityScript is a separate language and should be used only on questions using that language.`* – Draco18s no longer trusts SE Jan 08 '18 at 17:56
  • sorry im kind of new here. – Lian Levi Jan 11 '18 at 04:32

1 Answers1

1
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>();
Brandon Miller
  • 1,534
  • 1
  • 11
  • 16