2D game dialogue box

Hi,
I am doing some simple 2D game for a phone and I found on store some dialogue system called Narrate. It works fine, but when dialogue box appear and text is being written automatically it can’t disappear after that. Playing the game on my phone I can’t close this dialogue window. It is still visible. On PC you use Space for close/skip action. But in the code (any code of that system) I couldn’t find how to change that option for android game. So, I have idea, that it could just disappear by ‘close on click’ action, but when I add the button to the dialogue box and set it to ‘make active’ NO it destroys the window completely. So, how to close that window using touch, without destroying this completely?

SetActive(false) doesn’t destroy an object.
Can you copy the code that works on your pc when you hit the spacebar?
Then, I’m sure you can just set click = (do the same thing) for your android.

Sorry, I wasn’t available. So, it is much more complicated, because there is no reference to the spacebar in the code.
I come back to the first version of the conversation box - a simple one - and I have a simple code. It works and it solve the problem, but it has another problem :] When I press E key to skip conversation it shows dialogue window one after another and then disappear (as it should) but the machine is still looking for a text. When it refers to the empty line of the text from the text file it shows alert : Array index is out of range. Can you tell me just how to stop program to read if the line is empty and not reacting on E key?

    void Start () {

        player = FindObjectOfType<PlayerController> ();

        if(textFile != null)
        {
            textLines = (textFile.text.Split('\n'));
        }

        if (endAtLine == 0)
        {
            endAtLine = textLines.Length - 1;
        }
    }
    void Update()
    {
        theText.text = textLines[currentLine];

        if (Input.GetKeyDown (KeyCode.E))
        {
            currentLine += 1;
        }
        if (currentLine > endAtLine) {
            textBox.SetActive (false);
        }

}
}

Well, you didn’t include the variable declarations, so I can’t give specific tips/advice…
However, I can see from your example that you should set the text of “theText” at the beginning, and then update it when you get the E keycode. Stop setting the text in update like that. (which is incidentally probably the bug you’re having, also). :slight_smile:

Oh, sorry. This is the rest of the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextBoxManager : MonoBehaviour {

    public GameObject textBox;

    public Text theText;

    public TextAsset textFile;
    public string[] textLines;

    public int currentLine;
    public int endAtLine;

    public PlayerController player;

    void Start () {

I think the suggestion I made before will solve the issue. Did you try it?

I know what you are telling but I can’t understand it. What does it mean to set in at the beginning?

I mean in Start, you can set this:

 theText.text = textLines[currentLine];

and when you press the ‘e’ key, you can do:

 if (currentLine <= endAtLine) theText.text = textLines[currentLine];

And remove it from the spot you currently have it in Update().

Something like that. This way, the update function won’t be trying to read currentline when you’ve updated it just beyond the end, which is what was happening before.

Ok, I did two versions. The first one looks like that:

void Start () {

        player = FindObjectOfType<PlayerController> ();

        if(textFile != null)
        {
            textLines = (textFile.text.Split('\n'));
        }

        if (endAtLine == 0)
        {
            endAtLine = textLines.Length - 1;
        }

        theText.text = textLines[currentLine];

        if (currentLine > endAtLine) {
            theText.text = textLines [currentLine];
        }
    }
    void Update()
    {
       
        if (Input.GetKeyDown (KeyCode.E))
        {
            currentLine += 1;
        }
    }
}

…and it doesn’t work, because I missed something at the end of the code:

void Update()
    {
       
        if (Input.GetKeyDown (KeyCode.E))
        {
            currentLine += 1;
        }

        if (currentLine > endAtLine) {
            textBox.SetActive (false);
        }
    }
}

And now it works without an error but it counts every E. When I push E key couple of times, when the player come close to the enemy (who has a trigger for conversation box) nothing appears. When I don’t press E key the message box shows itself and disappears after 3 times pressed E. So, it counts E and do not shows the message from the text file one verse after another.

You didn’t try it like my example. Setting it to the first line was only in start.
My suggestion had been to update the current line, like you were doing with the ‘e’ key already, but then use the check I wrote:
This bit of code should be in the Update() after you check for the ‘e’ key and after you increment the current line.

if(currentLine <= endAtLine)  theText.text = textLines[currentLine];

You could change that a little to like this:

if(Input.GetKeyDown(KeyCode.E)) {
   if (currentLine < endAtLine) {
      currentLine++;
      theText.text = textLines[currentLine];
      if (currentLine == endAtLine) textBox.SetActive(false);
   }
}

And get rid of the extra check for endAtLine in your current Update().