Dialogue System w/ Gameobject [ ] ?
Hello, I'm looking for a advice I want to use "Gameobject [ ]" to turn on and off text objects in order. What method can be the answer? Example:
foreach (GameObject mirror in mirrors)
{
//turn on first mirror and turn on the next one
}
Answer by Hellium · Mar 02, 2019 at 06:05 PM
private IEnumerator EnableMirrors()
{
WaitForSeconds wait = new WaitForSeconds( 1 );
int i = 0;
mirrors[i].SetActive(true);
for( ; i < mirrors.Length - 1 ; i++ )
{
yield return wait;
mirrors[i].SetActive(false);
mirrors[i+1].SetActive(true);
}
yield return wait;
mirrors[i].SetActive(false);
}
Thanks! Okay, so I need to switch between lines by pressing the return button. The main thing, is that i need to change in not automatically, but from pressing the Return button, what must be changed or added? So the code looks like this for now:
public GameObject[] lines;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
StartCoroutine(Enable$$anonymous$$irrors());
}
}
private IEnumerator Enable$$anonymous$$irrors()
{
WaitForSeconds wait = new WaitForSeconds(1);
int i = 0;
lines[i].SetActive(true);
for (; i < lines.Length - 1; i++)
{
yield return wait;
lines[i].SetActive(false);
lines[i + 1].SetActive(true);
}
yield return wait;
lines[i].SetActive(false);
}
If you want the gameObjects to be activated by the press of a button
Replace the following line
WaitForSeconds wait = new WaitForSeconds( 1 );
By
WaitUntil wait = new WaitUntil( () => Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Return ) );
And add a Boolean to make sure the coroutine is not already running
private bool dialogueDisplayed;
void Update()
{
if (!dialogueDisplayed && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
StartCoroutine(Enable$$anonymous$$irrors());
}
}
private IEnumerator Enable$$anonymous$$irrors()
{
dialogueDisplayed = true;
WaitForSeconds wait = new WaitForSeconds(1);
int i = 0;
lines[i].SetActive(true);
for (; i < lines.Length - 1; i++)
{
yield return wait;
lines[i].SetActive(false);
lines[i + 1].SetActive(true);
}
yield return wait;
lines[i].SetActive(false);
dialogueDisplayed = false;
}
Okay, hope I write it right, when I press return button now, it jumps very fast from 1st to second line, and third, goes very fast to and 4th line is active by pressing the return button just a second time etc., this is the main issue for now. And the last thing is that when the last line is on, lines must be closed by pressing the return button, how can this be achieved? Thanks for your help.
public GameObject[] lines;
private bool dialogueDisplayed;
void Update()
{
if (!dialogueDisplayed && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
StartCoroutine(Enable$$anonymous$$irrors());
}
}
private IEnumerator Enable$$anonymous$$irrors()
{
dialogueDisplayed = true;
WaitUntil wait = new WaitUntil(() => Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return));
int i = 0;
lines[i].SetActive(true);
for (; i < lines.Length - 1; i++)
{
yield return wait;
lines[i].SetActive(false);
lines[i + 1].SetActive(true);
}
yield return wait;
lines[i].SetActive(false);
dialogueDisplayed = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612192811im_/https://answers.unity.com/themes/thub/images/avi.jpg)