D3NSHI
D3NSHI

Reputation: 13

Can't set public Text = public string[]

Cannot implicitly convert type 'string[]' to 'string' Hi I'm new to all this. I'm trying to make a monologue system for NPCs. I can't figure out how to make public Text = public string[] sentences. It comes up with an error.

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# Cannot implicitly convert type 'string[]' to 'string'

on "dialogText.text = sentences;" , "sentences" is underlines red and shows the error message above.

there was no red underline when I had just "public string sentences" instead of "public string[] sentences"

extra info: I put a text UI on the public text dialogText in Unity. I have [] with public string[] because i want there to be multiple lines of sentences.

idk

Upvotes: 0

Views: 180

Answers (1)

Gaurav Mall
Gaurav Mall

Reputation: 2412

You are getting an error because the text property needs a string and you are providing a string array. Even though the textbox is multiline you can't just pass in an array. What you need to do is this:

//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.
}

This should fix it.

Upvotes: 1

Related Questions