无法设置公共文本=公共字符串[]

时间:2019-06-30 08:32:27

标签: c# unity3d

无法将类型'string []'隐式转换为'string' 嗨,我是这一切的新手。我正在尝试为NPC建立独白系统。我不知道如何使公共文本=公共字符串[]句子。出现错误。

public class NPCDialogue : MonoBehaviour

[TextArea(3, 10)]
public string[] sentences;                              //THIS

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

}

void OnTriggerStay(Collider other)
{
    if (other.gameObject.name == "Sprite")
    {
        guiObject.SetActive(true);
        playerInRange = true;
        if (guiObject.activeInHierarchy == true && Input.GetButtonDown("Use"))
        {

            if (dialogBox.activeInHierarchy)
            {
                dialogBox.SetActive(false);                  
            }
            else
            {
                dialogBox.SetActive(true);
                dialogText.text = sentences;                    //THIS                        

CS0029 C#无法将类型'string []'隐式转换为'string'

在“ dialogText.text =句子;”上,“句子”用红色下划线标出并显示上面的错误消息。

当我只有“公共字符串句子”而不是“公共字符串[]句子”时,没有红色下划线

其他信息:我在Unity的公共文本dialogText上放置了一个文本UI。我有[]和公共字符串[],因为我希望句子多行。

idk

1 个答案:

答案 0 :(得分:1)

由于text属性需要一个字符串,并且正在提供一个字符串数组,因此您收到错误消息。即使文本框是多行,您也不能只传递数组。您需要做的是这样:

//Looping to get every item in the array
for(int i = 0; i < sequences.Length; i++) {
   dialogText.AppendText(sequences[i]); // This is how we add multi-line text. We append text so the next time we perform the action the text is going to be on a new line.
}

这应该解决它。

相关问题