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): var dialogue = false; var Text : GUIText; var dialogLines : String[]; function Start(){ if(dialogue){ //Show the first chunk of text, split lined if too long dialogLines = (Text.text.Split("\n"[0])); } } function Update(){ if(Input.GetKeyDown("return")){ //Show the next chunk of text, split lined if too long } } How can I go about this? Any help would be much appreciated!
After a bit more searching, my code has come down to this: Code (csharp): var dialogue = false; var Text : GUIText; var dialogLines : String; function Start(){ if(dialogue){ Text.text = dialogLines; } } It works, but is there a way I could turn the string variable into an array or have multiple lines?
Now I have my code up to this, but it only displays the last element I typed into the string.. Code (csharp): var dialogue = false; var Text : GUIText; var dialogLines : String; function Start(){ if(dialogue){ for(var item : String in dialogLines){ Text.text = item; } } I could really use some help with this if available.
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): var dialogue = false; var Text : GUIText; var dialogLines : String[]; //Make this an array var displayIndex = 0; function Update() { if(dialogue) { Text.text = dialogLines[displayIndex]; dialogue = false; } } From there you can just change the index and tick the dialogue checkbox to change the displayed text.
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): var dialogue = false; var Text : GUIText; var dialogLines : String[]; var displayIndex = 0; private var dialogToggle = true; function Update(){ if(dialogue){ Text.text = dialogLines[displayIndex]; } if(dialogue dialogToggle Input.GetKey("return")){ NextLine(); } if(displayIndex > dialogLines.length){ audio.PlayOneShot(confirm); Node(); Destroy(gameObject); } } function NextLine(){ displayIndex++; dialogToggle = false; yield WaitForSeconds(1); dialogToggle = true; }
Ok. I actually solved it btw, but now I'm having trouble with this: Code (csharp): if(displayIndex > dialogLines.length){ audio.PlayOneShot(confirm); Node(); Destroy(gameObject); } 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.
Because collections are 0-indexed the length is also out of range. Do >= length instead. Most basic example: Code (csharp): int[] myInts = new int[1] { 5 } Debug.Log(myInts.Length); //prints 1 Debug.Log(myInts[0]); // prints 5 Debug.Log(myInts[myInts.Length]); // throws Exception