C# 尝试从实例化的buttom预制获取组件属性时获取NullReferenceExption
我通过脚本实例化按钮,然后需要在父对象中调用一个脚本 这在预制脚本中,当单击其中一个按钮时调用。性能根本不相关,所以我只是告诉它从下到上,直到它到达附加了C# 尝试从实例化的buttom预制获取组件属性时获取NullReferenceExption,c#,unity3d,user-interface,C#,Unity3d,User Interface,我通过脚本实例化按钮,然后需要在父对象中调用一个脚本 这在预制脚本中,当单击其中一个按钮时调用。性能根本不相关,所以我只是告诉它从下到上,直到它到达附加了mllhildTestController.cs脚本的控制器游戏对象 public void ClickMe() { string parentNode = ReturnLabel(); string path = this.GetComponentInParent<mllhildTestController>()
mllhildTestController.cs
脚本的控制器游戏对象
public void ClickMe()
{
string parentNode = ReturnLabel();
string path = this.GetComponentInParent<mllhildTestController>().path;
this.GetComponentInParent<mllhildTestController>().Clear();
this.GetComponentInParent<mllhildTestController>().Load(path, parentNode);
}
控制器脚本
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);
}
}
}
公共类mllhildTestController:单行为
{
公共mllhildTestLinq linq;
公共mllhildTestDisplay;
公共mllhildTestButtons按钮;
公共列表列表列表;
公共字符串test=“smName”;
公共字符串smName=“mllhild”;
公共字符串displayText=“display.textWindow.font”;
公共字符串路径=@“C:\SlaveMaker4\Slaves\”;
//在第一帧更新之前调用Start
void Start()
{
明文();
//linq.LinqTest(@“C:\SlaveMaker4\Rhia.xml”);
//字符串文件名=@“C:\SlaveMaker4\Rhia.xml”;
//QueryXML(文件名,parentNode);
//Log(this.GetType().GetField(“test”).GetValue(this));
//Log(this.GetType().GetField(test).GetValue(this));
//Debug.Log(display.textWindow.font);
//Debug.Log(this.GetType().GetField(“显示”).GetType().GetField(“文本窗口”).GetType().GetField(“字体”).GetValue(this));
//Log(typeof(TextMeshProUGUI).GetProperty(displayText).GetValue(this));
//Log(this.GetType().GetField(displayText).GetValue(this));
}
//每帧调用一次更新
无效更新()
{
}
公共void SetText(字符串文本)
{
display.textWindow.text=文本;
}
公共无效添加文本(字符串文本)
{
display.textWindow.text+=文本;
}
公共无效明文()
{
display.textWindow.text=null;
}
公共游戏对象字段;
公共游戏对象AddNewButtoList(字符串标签)
{
GameObject b=实例化(bfield)为GameObject;
b、 SetActive(真);
b、 GetComponent().title.text=标签;
b、 transform.SetParent(bfield.transform.parent,false);
返回b;
}
公共空间清除()
{
foreach(listToStuff中的游戏对象b)
{
销毁(b);
}
listtostaff.Clear();
}
public void LoadButton(){Load(路径,“语言”);}
公共无效加载(字符串路径、字符串父节点)
{
字符串[]fileArray=Directory.GetFiles(路径“*.xml”);
foreach(fileArray中的字符串xmlfile)
{
GameObject blist=AddNewButtonList(xmlfile+“\n”+parentNode);
添加(blist);
//AddText(“\n\n”+xmlfile+”\n”);
//AddText(“父节点:“+parentNode+”\n”);
//AddText(“子节点:“+”\n”);
QueryXML(xmlfile,parentNode,blist);
}
}
}
您无法直接访问,因为您必须找到父项,然后才能找到所需的子项
因此,您的第一个挑战是找到组件的父级:
所以第一个命令是:GameObject.Find(“nameofbj”)
或GameObject.FindWithTag(“NameOfTag”)
因此,在您从这个游戏对象中找到所需的子组件之后,最后您可以访问链接到子组件的脚本方法,您不能直接访问,因为您必须找到父组件,然后才能找到所需的子组件 因此,您的第一个挑战是找到组件的父级: 所以第一个命令是:
GameObject.Find(“nameofbj”)
或GameObject.FindWithTag(“NameOfTag”)
因此,在您从这个游戏对象中找到所需的子组件之后,最后您可以访问链接到子组件的脚本方法。您能显示ReturnLabel()代码吗?MLLHILDTESTCOLLER是一个您将访问的脚本类?这是否回答了您的问题?对于Frenchy,在pinkflowydx33中添加了脚本,遗憾的是没有最终确定答案,请记住,如果您的原始问题已解决,请接受并投票支持答案,如果您有其他问题,请提出一个新问题:是否可以显示ReturnLabel()代码?MLLHILDTESTCOLLER是一个您将访问的脚本类?这是否回答了您的问题?对于Frenchy,在pinkflowydx33中添加了脚本,遗憾的是没有最终确定答案,请记住,如果您的原始问题已经解决,请接受并投票支持答案,如果您有其他问题,请提出新问题:完全正确。首先找到UIController游戏对象,然后从中选择“Controller”子对象;因为只有一个控制脚本。这恰恰解决了我的问题。首先找到UIController游戏对象,然后从中选择“Controller”子对象;因为只有一个控制脚本。这最终解决了我的问题
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);
}
}
}