【问题标题】:Getting NullReferenceExeption when trying to GetComponentInParent from a instantiated buttom prefab尝试从实例化的按钮预制件中获取组件InParent 时获取 NullReferenceExeption
【发布时间】:2020-08-30 22:32:06
【问题描述】:

我正在通过脚本实例化按钮,然后需要在父对象中调用更高级别的脚本

unity hierarchy

这是在预制脚本中,并在单击其中一个按钮时被调用。性能完全不相关,所以我只是告诉它从下往上直到它到达带有mllhildTestController.cs 脚本的控制器游戏对象。

public void ClickMe()
{
    string parentNode = ReturnLabel();
    string path = this.GetComponentInParent<mllhildTestController>().path;
    this.GetComponentInParent<mllhildTestController>().Clear();
    this.GetComponentInParent<mllhildTestController>().Load(path, parentNode);
}

但它只会导致错误

NullReferenceException:对象引用未设置为对象的实例

this.something 工作正常,所以在我的 GetComponentInParent&lt;mllhildTestController&gt;() 部分的逻辑中一定是错误的。

有人可以帮帮我吗?

编辑:这个功能似乎可以正常工作,但既然被问到了

public string ReturnLabel()
{
    return buttonText.text;
}

控制器脚本

public class mllhildTestController : MonoBehaviour
{
    public mllhildTestLinq linq;
    public mllhildTestDisplay display;
    public mllhildTestButtons buttons;
    public List<GameObject> listToStuff;

    public string test = "smName";
    public string smName = "mllhild";

    public string displayText = "display.textWindow.font";
    public string path = @"C:\SlaveMaker4\Slaves\";

    // Start is called before the first frame update
    void Start()
    {
        ClearText();

        // linq.LinqTest(@"C:\SlaveMaker4\Rhia.xml");
        // string filename = @"C:\SlaveMaker4\Rhia.xml";
        // linq.QueryXML(filename, parentNode);

        // Debug.Log(this.GetType().GetField("test").GetValue(this));
        // Debug.Log(this.GetType().GetField(test).GetValue(this));
        // Debug.Log(display.textWindow.font);
        // Debug.Log(this.GetType().GetField("display").GetType().GetField("textWindow").GetType().GetField("font").GetValue(this));
        // Debug.Log(typeof(TextMeshProUGUI).GetProperty(displayText).GetValue(this));
        // Debug.Log(this.GetType().GetField(displayText).GetValue(this));
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void SetText(string text)
    {
        display.textWindow.text = text;
    }

    public void AddText(string text)
    {
        display.textWindow.text += text;
    }

    public void ClearText()
    {
        display.textWindow.text = null;
    }

    public GameObject bfield;

    public GameObject AddNewButtonList(string label)
    {
         GameObject b = Instantiate(bfield) as GameObject;
         b.SetActive(true);
         b.GetComponent<PrefabListButtoms>().title.text = label;
         b.transform.SetParent(bfield.transform.parent, false);
         return b;
    }

    public void Clear()
    {
        foreach(GameObject b in listToStuff)
        {
            Destroy(b);
        }

        listToStuff.Clear();
    }

    public void LoadButton() { Load(path, "Language" ); }

    public void Load(string path, string parentNode)
    {
        string[] fileArray = Directory.GetFiles(path, "*.xml");

        foreach (string xmlfile in fileArray)
        {
            GameObject blist = AddNewButtonList(xmlfile + "\n" + parentNode);
            listToStuff.Add(blist);
            //AddText("\n\n" + xmlfile + "\n");
            //AddText("Parent-Node:" + parentNode + "\n");
            //AddText("Child-Nodes:" + "\n");

            linq.QueryXML(xmlfile, parentNode, blist);
        }
    }
}

【问题讨论】:

标签: c# unity3d user-interface


【解决方案1】:

你不能直接访问,因为你必须找到父母,然后你才能找到想要的孩子

所以您的第一个挑战是找到组件的父级:

所以第一个命令是:GameObject.Find("NameofObj")GameObject.FindWithTag("NameOfTag")

所以在你从这个游戏对象中找到想要的子组件之后..最后你可以访问链接到子的脚本方法

【讨论】:

  • 没错。首先找到 UIController GameObject,然后从中选择“Controller”子对象。
  • 我最终使用了 FindObjectOfType().AddValueVar01(1);因为只有一个控制脚本。这最终解决了我的问题youtu.be/ymq2AUckws0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2016-08-04
  • 2019-05-12
相关资源
最近更新 更多