3
\$\begingroup\$

i'm trying to print a string letter by letter for UI Text of Unity. With each click of a button, the Stampa function, must print a letter. Up to this point everything is working perfectly. But if I try to print a newline "\n" the function ignores him and continues to print on the same line.
This is my code:

using UnityEngine;
using UnityEngine.UI;

public class Click : MonoBehaviour {

    public static Text txt;
    public static int indice = 0;
    public string asd = "String1 \nStriga2";

    void Awake() {
        txt = GameObject.Find("Cmd").GetComponentInChildren<Text>();
    }

    public void Clicked() {
        Stampa(asd ,indice);
        indice++;
    }

    void Stampa(string stringa, int i) {
        txt.text += stringa[i];
    }

}

That produces this result(unwanted): enter image description here

I also tried to print the entire string at the same time (not a letter at a time) as follows:

txt.text = "String1 \nStriga2";

That produces this result(wanted): enter image description here
How can I get this second result by printing letter by letter?

\$\endgroup\$
  • 1
    \$\begingroup\$ Check if string[i] is a slash, if it is then add both i and i+1 \$\endgroup\$ – DH. Jul 18 '16 at 18:01
1
\$\begingroup\$

It's reading one character at a time, so it's not properly escaping the new line. I would suggest looking for escape characters in your Stampa() function, and if it's an escape character, read the next character in at the same time. Something like:

void Stampa(string stringa, int i) {
    string r = stringa[i];
    if (r == '\\')
       r += stringa[i+1];
    txt.text += r;
}

Thought you'd likely need to return the index value to the calling function. You could return an int and maybe do something like this:

public void Clicked() {
        int val = Stampa(asd, indice);
        indice += val;
    }

int Stampa(string stringa, int i) {
        int ret = 1;
        string s = stringa[i];
        if (s == '\\')
        {
           s += stringa[i+1];
           ret++;
        }
        txt.text += s;

        return ret;
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice solution, I have not assessed the fact that he read '\n' separately. \$\endgroup\$ – Amarildo Jul 18 '16 at 19:30
  • \$\begingroup\$ It's an easy miss since dealing with larger strings it's basically read in as a single character. I'm glad it helped! \$\endgroup\$ – Jesse Williams Jul 18 '16 at 19:39
  • \$\begingroup\$ @Amarildo, I accepted your change from string to char, then reverted the change back because the value of s can be more than one character - that was the whole point of the added code, to allow string s = "\\nS" or something similar, right? Two character, the newline and the next character in from your asd value. \$\endgroup\$ – Jesse Williams Jul 18 '16 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.