Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dialogue GUI Text

Discussion in 'Scripting' started by Cooper37, Apr 2, 2014.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Hi, Unity Community! I've been working on a simple dialogue system for a while now, but it hasn't gone too much of anywhere...I didn't want to create numerous GUIText for each line since the scene already contains hundreds of stuff! So, I'm trying to work with one GUI Text and having a script where you can type sentences into a string. But, there's some parts missing:

    Code (csharp):
    1.  
    2. var dialogue = false;
    3. var Text : GUIText;
    4. var dialogLines : String[];
    5.  
    6. function Start(){
    7.      if(dialogue){
    8.           //Show the first chunk of text, split lined if too long
    9.           dialogLines = (Text.text.Split("\n"[0]));
    10.      }
    11. }
    12.  
    13. function Update(){
    14.      if(Input.GetKeyDown("return")){
    15.           //Show the next chunk of text, split lined if too long
    16.      }
    17. }
    18.  
    How can I go about this? Any help would be much appreciated! :)
     
  2. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    After a bit more searching, my code has come down to this:

    Code (csharp):
    1.  
    2. var dialogue = false;
    3. var Text : GUIText;
    4. var dialogLines : String;
    5.  
    6. function Start(){
    7.      if(dialogue){
    8.          Text.text = dialogLines;
    9.      }
    10. }
    11.  
    It works, but is there a way I could turn the string variable into an array or have multiple lines?
     
  3. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Now I have my code up to this, but it only displays the last element I typed into the string..
    Code (csharp):
    1.  
    2. var dialogue = false;
    3. var Text : GUIText;
    4. var dialogLines : String;
    5.  
    6. function Start(){
    7. if(dialogue){
    8.     for(var item : String in dialogLines){
    9.         Text.text = item;
    10.     }
    11. }
    12.  
    I could really use some help with this if available.
     
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    you're looking for how to create arrays right ? http://docs.unity3d.com/Documentation/ScriptReference/Array.html

    So your code could become something like:

    Code (csharp):
    1.  
    2. var dialogue = false;
    3. var Text : GUIText;
    4. var dialogLines : String[]; //Make this an array
    5. var displayIndex = 0;
    6.  
    7. function Update() {
    8.     if(dialogue) {
    9.         Text.text = dialogLines[displayIndex];
    10.         dialogue = false;
    11.     }
    12. }
    13.  

    From there you can just change the index and tick the dialogue checkbox to change the displayed text.
     
  5. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Okay. After working with this for a while, I came up with a way to show each individual line whenever you press 'Enter'. The code looks flawless, but now the first index/string isn't showing. Do you guys see something in my script that I don't? Thanks :)

    NOTE that I moved all of my code from the Start function to an Update function and a 'custom' function:
    Code (csharp):
    1.  
    2. var dialogue = false;
    3. var Text : GUIText;
    4. var dialogLines : String[];
    5. var displayIndex = 0;
    6.  
    7. private var dialogToggle = true;
    8.  
    9. function Update(){
    10.     if(dialogue){
    11.         Text.text = dialogLines[displayIndex];
    12.     }
    13.     if(dialogue  dialogToggle  Input.GetKey("return")){
    14.         NextLine();
    15.     }
    16.     if(displayIndex > dialogLines.length){
    17.         audio.PlayOneShot(confirm);
    18.         Node();
    19.         Destroy(gameObject);
    20.     }
    21. }
    22.  
    23. function NextLine(){
    24.     displayIndex++;
    25.     dialogToggle = false;
    26.     yield WaitForSeconds(1);
    27.     dialogToggle = true;
    28. }
    29.  
     
  6. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Ok. I actually solved it btw, but now I'm having trouble with this:
    Code (csharp):
    1.  
    2.     if(displayIndex > dialogLines.length){
    3.         audio.PlayOneShot(confirm);
    4.         Node();
    5.         Destroy(gameObject);
    6.     }
    7.  
    It says, "IndexOutOfRangeException: Array index is out of range." In other words, how can I detect if the index is out of range in script and then destroy the gameObject.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because collections are 0-indexed the length is also out of range. Do >= length instead.

    Most basic example:
    Code (csharp):
    1.  
    2. int[] myInts = new int[1] { 5 }
    3.  
    4. Debug.Log(myInts.Length); //prints 1
    5. Debug.Log(myInts[0]); // prints 5
    6. Debug.Log(myInts[myInts.Length]); // throws Exception
    7.  
     
  8. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    That worked out just fine! Thanks! :)