【发布时间】:2020-08-30 22:32:06
【问题描述】:
我正在通过脚本实例化按钮,然后需要在父对象中调用更高级别的脚本
这是在预制脚本中,并在单击其中一个按钮时被调用。性能完全不相关,所以我只是告诉它从下往上直到它到达带有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<mllhildTestController>() 部分的逻辑中一定是错误的。
有人可以帮帮我吗?
编辑:这个功能似乎可以正常工作,但既然被问到了
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);
}
}
}
【问题讨论】:
-
你能显示 ReturnLabel() 代码吗?并且 mllhildTestController 是您要访问的脚本类?
-
到Frenchy,将脚本添加到pinkflowydx33,遗憾的是没有
-
要最终确定答案,如果您的原始问题已解决,请记住接受并投票赞成答案,如果您有其他问题,请提出新问题:stackoverflow.com/help/someone-answers
标签: c# unity3d user-interface