Having issues with Dialog boxes

Basically I decided to add dialog boxes so as to add a story narrative to the game, however for the moment the tutorial I’m following has been leading me to problems, possibly brought on by a newer version of Unity.

So far my code below is what I’ve come up with. I’m also hoping to have it so that the game is paused when the dialog appears (since I don’t want the player to get shot when s/he is reading) and I think I’ve cracked that part, however why there is a compiler issues I’m uncertain.

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

  public class TextBoxManager : MonoBehaviour {

public GameObject DialogBox;
public Text Dlog;

public TextAsset Dialog;
public string[] DialogLines;
public int StartOfLine;
public int EndOfLine;

public PlayerController player;

void Start()
{
    player = FindObjectOfType<PlayerController>();
    if (Dialog != null)
    {
        DialogLines = (Dialog.text.Split('\n'));
        Time.timeScale = 0;
    }
    if (EndOfLine == 0)
    {
        EndOfLine = DialogLines.Length - 1;
    }

}
void Update()
{
    Dialog.text = DialogLines[StartOfLine];
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartOfLine += 1;
    }
    if (StartOfLine > EndOfLine)
    {
        DialogBox.SetActive(false);
    }
}
 }

What is the compiler error?

@Rob property or indexer cannot be assigned to unity.asset.text it is read only

Edit:

Text Box Manager.cs (34,16) Error CS0200: Property or Intexer UnityEngine.TextAsset.Text cannot be assigned to (it is read only)

Is that the whole script, and the one that is generating the error Aron? Which line number? Screenshot would be useful…

@Rob Sorry just updated the exact issue about. The line it appears to have problems with is this one:

Dialog.text = DialogLines[StartOfLine];

Thanks…

As I understand it, you have declared Dialog of type TextAsset, not having watched the tutorial, I am assuming this is a raw text file you have as an asset which is being read in.

On line 34 you then try to set it’s text property;

Dialog.text = DialogLines[StartOfLine];

I’m guessing what you are trying to do is output the text from the raw file to a UI Text object in the scene, if so, I believe you named that Dlog in your code.

Thus, try;

Dlog.text = DialogLines[StartOfLine];

1 Like

Ahhh I see what my problem was…it’s the problem I often run in when I give my own titles to commands instead of simply following. That seems to have fixed it, now hopefully I can have it paused when it starts and then start after it goes.

This reminds me of the Text101 section…

Text text;

text.text = "This is some text, how many times can I say text";

:slight_smile:

Perhaps consider renaming some of your variables… for example, the variable for the TextAsset, that’s your raw text, or source text, perhaps consider one of those as a name for it, or just textAsset :slight_smile:


Updated Wed Sep 27 2017 01:18

You could consider popping a little check around that line in the Update method also, to see if the text has already been set, other wise you are hitting that array and populating that UI Text object in the scene many, many times and unnecessarily. Just a thought :slight_smile:

1 Like

new problem: IndexOutOfRangeException:Arrey Index is Out of Range. Line 34 again

Use Debug.Log() to output the value of StartOfLine and also the length of DialogLines.

Not certain where to have put that…but I did place two different Debugs and only one appears to have shown up in the log.

    if (Dialog != null)
    {
        DialogLines = (Dialog.text.Split('\n'));
        Debug.Log("Start Problem");
    }

However this one didn’t register:

    if (EndOfLine == 0)
    {
        EndOfLine = DialogLines.Length - 1;
        Debug.Log("End Problem");
    }

…here I was thinking that something like a dialog box would be easy…

I think at the moment the code is prone to some issues…

For example…

You are attempting to read the text file in, splitting it by the escape newline.

Lets say for example this is the raw text;

This is line one

This is line three, there are two newline characters in this text block

So, hypothetically, you would have an array consisting of three items.

In your Update method, I would hazard a guess that when you press the space bar, even with ninja like reflexes, it will increment your StartOfLine variable to be greater than these three lines of text.

Need to really break this down a bit…

Firstly, I’d comment out all of the content in the Update method.

Lets actually check you have something in the array first.

So, in the Start method, change your Debug.Log statement to something like this;

Debug.Log("Array DialogLines has " + DialogLines.Length.ToString() + " lines of text");

What do you get back? Does this correspond with your raw text file asset?

1 Like

Claims it has 4 lines of text and nothing else is reported.

The problem appears after I press space after the end of the message where the box stays on screen. That is what I’m thinking might be part of the issue here…

Ok, great… so we now know that the \n is handling the newlines correctly.

The if statement is true, EndOfLine gets set to the length of the array, minus 1 (to handle the fact it is zero-based), so EndOfLine = 3 and at this point StartOfLine = 0.

Moving on to the Update method.

void Update()
{
    Dialog.text = DialogLines[StartOfLine];
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartOfLine += 1;
    }
    if (StartOfLine > EndOfLine)
    {
        DialogBox.SetActive(false);
    }
}

The first time the Update method is called it sets the Dlog text property to the first line of text.

Check for a space bar press and increments StartOfLine.

Lets put a Debug.Log() in the Update method, so we can see the value of StartOfLine, and we’ll put a Debug.Log() in after the increment so that you can see the values after they have been incremented…I would recommend commenting out the second if statement for now, thus;

void Update()
{
    Debug.Log("StartOfLine = " + StartOfLine.ToString());

    Dialog.text = DialogLines[StartOfLine];
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartOfLine += 1;
        Debug.Log("Space Bar Pressed : StartOfLine = " + StartOfLine.ToString());
    }
    // if (StartOfLine > EndOfLine)
    // {
        // DialogBox.SetActive(false);
    // }
}

Lets see what you get as output (anything over 3 is going to be bad).

Funny you should say that. I got Start of line = 1, 2, 3, 4

How many times did you press the space bar?

Four times…it seems to register each line properly…

Ok, that’s good… so the issue then is that at the beginning of the Update method where you access the array, your index value is greater than the number of elements it has…

void Update()
{
    Dialog.text = DialogLines[StartOfLine];
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartOfLine += 1;
    }
    if (StartOfLine > EndOfLine)
    {
        DialogBox.SetActive(false);
    }
}

So, we need to add some code to protect against that from happening and not just assume StartOfLine will be within the correct boundaries… perhaps something similar to this?

void Update()
{
    if (StartOfLine <= EndOfLine)  // this would allow for indexes 0, 1, 2 and 3 (length of 4)
    {
        Dialog.text = DialogLines[StartOfLine];

        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartOfLine += 1;
        }
    }
    else
    {
        Debug.Log("I am now out of text, so we can close the dialog box");
        // DialogBox.SetActive(false);
    }
}

Only problem with that code was Dialog.text was actually Dlog, but apart from that the Debug never showed up at all…

1 Like

lol… of course… hehe… serves me right for copying and pasting chunks of code from further up the thread :slight_smile:

The Debug not showing up is a problem then, as you would want it to in order for you to be able to close the dialog. So, the reason for that then is this;

  • Step 1
    StartOfLine = 0
    First line of text appears automatically
    Space bar pressed
    StartOfLine = 1

  • Step 2
    Second line of text appears
    Space bar pressed
    StartOfLine = 2

  • Step 3
    Third line of text appears
    Space bar pressed
    StartOfLine = 3

  • Step 4
    Fourth line of text appears

    No need to press the space bar, no indication to user to do so…
    StartOfLine never reaches 4 and thus the else is not triggered.


You could add a “Space to close” message, that might be quite nice…

void Update()
{
    if (StartOfLine <= EndOfLine)  // this would allow for indexes 0, 1, 2 and 3 (length of 4)
    {
        Dlog.text = DialogLines[StartOfLine];

        if (StartOfLine == EndOfLine)
        {
            Dlog.text += "\n\n";
            Dlog.text += "Press space to close";
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartOfLine += 1;
        }
    }
    else
    {
        Debug.Log("I am now out of text, so we can close the dialog box");
        // DialogBox.SetActive(false);
    }
}

I think the above would do it… getting tired now, so apologies if not… but after the 4th line of text appears, you should also get the prompt to press the space bar… when the player does so, the Debug message should appear.

If you don’t want the prompt to the play about pressing space, then we just need to move where the increment is in this loop so that on the next call of Update, the Debug is displayed.