A (X) amount of time between "dialogues" strings
Hello everyone, it's my first time here, so if I'am posting that on the wrong place or something like that, please tell me.
I'am trying to put some interval between the dialogues that I will put on Editor, instead of press some key to advance the dialogue, I want to put like "10" seconds of duration for every string in dialogue[].
So, it's basically about dialogue script, that when the "Player" enter on a Trigger2D zone activate the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Active_Dialog : MonoBehaviour
{
public string[] dialogue;
public Text output;
private int curLine;
void Start()
{
output.enabled = false;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
output.enabled = true;
output.text = dialogue[0];
}
}
void OnGUI()
{
if (output.enabled == true)
{
curLine++;
if (curLine < dialogue.Length)
{
output.text = dialogue[curLine];
}
else
{
curLine = 0;
output.enabled = false;
}
}
}
I already tried somethings like put a Coroutine, I tried in many lines to put a Coroutine with differentes blocks of code, the max that I got was the first and the second string of the array stay the amount of time I wanted. But I still messy about how to use Coroutine (since everywhere say a different thing to another one) and I'am pretty sure that I dont got it the idea of "pause and execute the next line of the code in x amount of seconds" yet... So if someone could explain how I can make that, I will be pretty thankful!
Answer by The_Three_Vs · Mar 31, 2020 at 07:55 PM
Have you tried using a boolean to track whether the coroutine is running? For example:
void OnGUI()
{
if (output.enabled == true && timerRunning == false)
{
StopAllCoroutines();
curLine++;
if (curLine < dialogue.Length)
{
output.text = dialogue[curLine];
StartCoroutine(Timer(10f));
}
else
{
curLine = 0;
output.enabled = false;
}
}
}
private bool timerRunning = false;
IEnumerator Timer(float time)
{
timerRunning = true;
yield return new WaitForSeconds(time);
timerRunning = false;
}
Hope this helps!
Your answer
Follow this Question
Related Questions
Iterate through dialogue array and display in game 1 Answer
Assigning a List string element from script A to another string variable in scriptA from scriptB 1 Answer
can't save values from another class to a list 1 Answer
How do I view an array that is inside of an array in the unity inspector? 0 Answers