c# - How to control gui text using the space bar in Unity? -


(disclaimer: new unity please bear me)

basically want have text element displays multiple sentences 1 letter @ time , stop @ end of sentence. also, if space bar pressed remaining text sentence should display immediately, if sentence finished next sentence should show.

so far have managed text display 1 letter @ time using strings stored in array. however, having difficulty navigating index index using space bar , getting complete sentence show if space bar pressed.

the code below:

using unityengine; using unityengine.ui; using system.collections;  public class textscript : monobehaviour {  public animator bar; public float letterpause = 0.1f; string[] strarray = new string[3]; string str; int i; int count;  void start () {     bar.enabled = true;      strarray[0] = "hello , welcome game";     strarray[1] = "the next line of code";     strarray[2] = "testing space bar";     gameobject.getcomponent<text> ().text = "";     startcoroutine(typetext ()); }  void update () {     if (input.getkeydown ("space")) {         gameobject.getcomponent<text> ().text = "";         count = count + 1;         = + 1;         typetext();     } }  ienumerator typetext () {     (i = 0; < strarray.length; i++) {         str = strarray[i];         if (i == count) {             foreach (char letter in str.tochararray()) {                 gameobject.getcomponent<text> ().text += letter;                 yield return new waitforseconds (letterpause);             }         }     } } 

}

so, not sure best way go achieving want. great!

i don't have unity on box test try this

public float letterpause = 0.1f; public float sentencepause = 2.0f; string[] strarray = new string[3]; bool skiptonext = false;  void start () {      strarray[0] = "hello , welcome game";     strarray[1] = "the next line of code";     strarray[2] = "testing space bar";     gameobject.getcomponent<text> ().text = "";     startcoroutine(typetext ()); }  void update () {     if (input.getkeydown ("space")) {         skiptonext = true;     } }  ienumerator typetext () {     (int = 0; < strarray.length; i++)      {         foreach (char letter in strarray[i].tochararray())          {             if (skiptonext)             {                 gameobject.getcomponent<text> ().text = strarray[i];                 skiptonext = false;                 break;             }              gameobject.getcomponent<text> ().text += letter;             yield return new waitforseconds (letterpause);         }         yield return new waitforseconds (sentencepause);         gameobject.getcomponent<text> ().text = "";     }      gameobject.getcomponent<text> ().text = strarray[strarray.length - 1]; } 

edited reflect comment updates.


Popular posts from this blog