I’m working in a little app in Unity using UI, and I want to learn/know how to use the IEnumerator to stack multiple yield.
My goal is to use coroutines to disable UI elements after a short time pressing a button
This is my code so far:
using System.Collections;
using UnityEngine;
public class Script_EnableDisableGo : MonoBehaviour
{
[Header("BackGround canvas")]
public GameObject disableBg;
[Header("ForeGround canvas")]
public GameObject disableFg;
[Header("Wait before disable")]
public float timeToWait;
public void DisableBackGroundCanvas()
{
StartCoroutine(Wait());
}
public void DisableForeGroundCanvas()
{
StartCoroutine(Wait());
}
IEnumerator Wait()
{
yield return new WaitForSeconds(timeToWait);
disableBg.SetActive(false);
yield return new WaitForSeconds(timeToWait);
disableFg.SetActive(false);
}
}
Main behaviour:
After pressing a button, a UI element shows and the previous get disabled, and after pressing another button the disabled UI element shows enabled
Main UI window is visible (press button) [wait
for 1 sec] → New UI window is visible | Main UI
window is disabled
New UI window is visible (press return
button)[wait for 1 sec] → Main UI
window is visible | New Ui window is disabled
When I run this coroutine, those elements are disabled at the same time, and I just want to disable one at a time
From your code and your explanation of what you are attempting to do this is what I currently understand (correct me if I get anything wrong)
You have 2 windows named MainWindow (activated) and NewWindow (deactivated).
You have 2 buttons “Hide MainWindow Button” (enabled) and “Hide NewWindow Button” (disabled)
When you press the MainWindow button you want to
Disable MainWindow button (where this is done is not defined)
Enable NewWindow button (where this is done is not defined)
Deactivate the MainWindow
wait one second
Activate the NewWindow
wait one second
4. When you press the NewWindow button you want to
Disable NewWindow button (where this is done is not defined)
Enable MainWindow button (where this is done is not defined)
Deactivate the NewWindow
wait one second
Activate the MainWindow
wait one second
Then it seems that all you need to do is this
using System.Collections;
using UnityEngine;
public class Script_EnableDisableGo : MonoBehaviour
{
[Header("BackGround canvas")]
public GameObject backGroundCanvas; // main window???
[Header("ForeGround canvas")]
public GameObject foreGroundCanvas; // new window???
[Header("Wait before disable")]
public float timeToWait;
public void DisableBackGroundCanvas()
{
StopCoroutine("Wait");
StartCoroutine(Wait(backGroundCanvas, foreGroundCanvas));
}
public void DisableForeGroundCanvas()
{
StopCoroutine("Wait");
StartCoroutine(Wait(foreGroundCanvas, backGroundCanvas));
}
IEnumerator Wait(GameObject go1, GameObject go2)
{
yield return new WaitForSeconds(timeToWait);
go1.SetActive(false);
yield return new WaitForSeconds(timeToWait);
go2.SetActive(true);
}
}
Or instead of having two functions (DisableBackGroundCanvas/DisableForeGroundCanvas) you can do everything in one function like this (this also toggles the buttons)
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Script_EnableDisableGo : MonoBehaviour
{
[Header("BackGround canvas")]
public GameObject backGroundCanvas;
[Header("ForeGround canvas")]
public GameObject foreGroundCanvas;
[Header("Main Window Button")]
public Button MainWindowButton; // rename this to your liking
[Header("New Window Button")]
public Button NewWindowButton; // rename this to your liking
[Header("Wait before disable")]
public float timeToWait;
public void DisableEnableWindows() // rename this to your liking
{
if ((MainWindowButton != null) && (NewWindowButton != null))
{
MainWindowButton.enabled = !MainWindowButton.enabled;
NewWindowButton.enabled = !NewWindowButton.enabled;
}
StopCoroutine("Wait");
if (backGroundCanvas.activeSelf)
StartCoroutine(Wait(backGroundCanvas, foreGroundCanvas));
else
StartCoroutine(Wait(foreGroundCanvas, backGroundCanvas));
}
IEnumerator Wait(GameObject go1, GameObject go2)
{
yield return new WaitForSeconds(timeToWait);
go1.SetActive(false);
yield return new WaitForSeconds(timeToWait);
go2.SetActive(true);
}
}