- Home /
URGENT: Simple Dialogue Trigger Situation. Need Help Please.
Hey guys, I'll get straight to the point and try my hardest to explain the situation. So basically I use youtube to understand scripting and follow along with the code. What I want to do is a simple dialogue system such as when the player hits a collider with a trigger in it, some dialogue typing appears on the screen. I press a button to progress through the conversations until the end. But when I leave the collider (I guess OnTriggerExit) OR when the conversation finishes, I want the dialogue to disappear and reset so that if I touch the collider again, it repeats the whole conversation again from the beginning. I have been working on this for four days now but am ready to give up.
This is what I have so far, but it doesn't work the way I described up there.
public class Dialogue : MonoBehaviour {
public Text textArea;
public string[] strings;
public float speed = 0.3f;
int stringIndex = 0;
int characterIndex = 0;
private bool display = false;
void OnTriggerEnter(Collider collider){
if (collider.transform.name == "Player")
{
display = true;
StartCoroutine (DisplayTimer ());
}
}
void OnTriggerExit (Collider collider){
if (collider.transform.name == "Player")
{
display = false;
}
}
IEnumerator DisplayTimer(){
while (1 == 1) {
yield return new WaitForSeconds (speed);
if (characterIndex > strings [stringIndex].Length) {
continue;
}
textArea.text = strings [stringIndex].Substring (0, characterIndex);
characterIndex++;
}
}
void Update() {
if (Input.GetKeyDown (KeyCode.E)) {
if (characterIndex < strings [stringIndex].Length) {
characterIndex = strings [stringIndex].Length;
} else if (stringIndex < strings.Length) {
stringIndex++;
characterIndex = 0;
}
}
}
}
So just to give you guys some information on the code, the IENumerator and Update is mostly from the youtuber I watching since he showed to display text on the screen but similar to like Pokemon, where each letter progresses and if I press a button I display everything. THAT PART WORKS FINE, when i enter the trigger box, the texts does appear just like I have written it. Also, he didn't have any of the trigger stuff. I wanted that to describe what I mentioned up there.
Main Problems: -When conversation finishes, make the dialogue stop instead of continuing in an ArrayOutOfIndex problem. -Get the Dialogue to stop appearing when I exit the trigger. -When I enter it again, it should show me the dialogue again from the beginning. -After having fixed all this, if I was to set up another dialogue trigger box somewhere else, is it possible to change the conversation to something different while keeping the same functions
Your answer
Follow this Question
Related Questions
Adding dialouge 1 Answer
Waiting for Input via Coroutine 2 Answers
[SOLVED] Accessing Specific Instance of Variable on Script Across Multiple Objects 0 Answers
remove tab and changing text in Custom Resolution Dialog box? 0 Answers
Getting a dictionary to recognize a variable string defined elsewhere 0 Answers