Unity 找不到其他脚本 NullReferenceException:对象引用未设置为对象的实例

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

所以我的 TowerNode 脚本无法找到 Shop 脚本并且出于某种原因激活或禁用它它只是找不到它所以它给我遇到 "NullReferenceException: 对象引用未设置为对象的实例 TowerNode.OnMouseDown ()(位于 Assets/Scripts/TowerNode.cs:46)"

它不能使第 46 行的 Shop.Enable 方法

这是 TowerNode 脚本

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

这是 商店 脚本:

使用UnityEngine; 使用 UnityEngine.UI;

public class 店铺: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);
}}
shop = GetComponent<Shop>();
    GameObject.Find("Shop");

我假设 Shop 脚本驻留在名为 Shop 的游戏对象上,而不是 TowerNode GameObject,这意味着您应该像这样从该游戏对象获取 Shop 脚本:

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