Unity UI GetComponent

时间:2019-06-15 16:21:41

标签: c# user-interface unity3d

我正在制作一个对话框系统,所以我创建了一个预制件,其预制件包括:主角色肖像(图像),NPC肖像(图像),将在其中显示文本的图像,最后一个是文本。在我的脚本中,我传递了这个预制件,并将其保存在名为dialog_panel的变量中。用代码创建预制件后,我想更改第一个短语的文本,即我的NPC。但它崩溃了:

  

“ GetComponent要求所请求的组件'TextElement'从MonoBehaviour或Component派生,或者是一个接口。   UnityEngine.GameObject.GetComponent [T]()(在C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28)   Interactivity.Update()(位于Assets / Scripts / Interactivity.cs:74)“

我尝试过尝试获取UI文本/标签的组件文本,但是D:是不可能的。我的统一版本是:2019.3.0.a5

这是我的代码,注释的内容也给了我同样的错误:

dialog_panel = Instantiate(copy_panel);
dialog_panel.transform.SetParent(FindObjectOfType<Canvas>().transform, false);
dialog_panel.transform.position = new Vector3(512, 99, dialog_panel.transform.position.z);
//dialog_panel.transform.Find("Text").GetComponent<Label>().text = "HOLAA";
/*GameObject obj = GameObject.Find("NPCText");
TextElement instruction = obj.GetComponent<TextElement>();
instruction.text = "HI";
//obj.GetComponent<TextElement>().text = "HI";*/
// dialog_panel.transform.Find("Text").GetComponent<Label>().text = "HI"; 
dialog_panel.transform.Find("Text").GetComponent<TextElement>().text = "HI";

0 个答案:

没有答案
相关问题