- Home /
Mouse Idle UI question
How might I refactor my code to allow a mouse-idle Coroutine take into account whether a submenu is currently active or not?
In my program, I have a couple of menu buttons that call a submenu (an object and its children, repurposed for each menu "type"). As well, I have a coroutine that changes the UI's alpha to transparent after a period of mouse inactivity (and back to opaque on movement). I am trying to:
1. Reuse the same Fade() coroutine for my submenu for when it is activated/deactivated.
2. Have the ability to include/exclude the submenu from Fade() as a whole dependent on if it is activated.
bool boop;
public void HideSUB()
{
boop = true;
if (GetComponent<UIHandling>().SubOn == false)
{
StartCoroutine(Fade(true));
}
else
StartCoroutine(Fade(false));
boop = false;
}
IEnumerator Fade(bool fading)
{
CanvasGroup UIalpha = GameObject.Find("UIPanel").GetComponent<CanvasGroup>();
CanvasGroup SUBalpha = GameObject.Find("Submenu").GetComponent<CanvasGroup>();
// Fade Out
if (fading == true)
{
if (boop == false)
{
while (UIalpha.alpha > 0) //while opaque
{
UIalpha.alpha -= Time.deltaTime / 2; //fades over time
if (GetComponent<UIHandling>().SubOn == true)
{
while (SUBalpha.alpha > 0)
{
SUBalpha.alpha -= Time.deltaTime / 2;
}
}
yield return null;
}
}
else
{
while (SUBalpha.alpha > 0)
{
SUBalpha.alpha -= Time.deltaTime / 1;
}
yield return null;
}
}
// Fade In
else if (fading == false)
{
if (boop == false)
{
while (UIalpha.alpha < 1)
{
UIalpha.alpha += Time.deltaTime / 1;
if (GetComponent<UIHandling>().SubOn == true)
{
while (SUBalpha.alpha < 1)
{
SUBalpha.alpha += Time.deltaTime / 1;
}
}
yield return null;
}
}
else
{
while (SUBalpha.alpha < 1)
{
SUBalpha.alpha += Time.deltaTime / 1;
}
yield return null;
}
}
Using the (non-working) code above, I tried to include some while/else/ifs into my working Fade() to incorporate the changes I'm trying to make above. I keep getting NullReferenceExceptions at the line checking if my bool "SubOn" is true/false.
Any help would be appreciated! Thanks.
Your answer

Follow this Question
Related Questions
Canvas with mask and transparent image 1 Answer
Use stencil buffer to hide objects with same shader? 1 Answer
Create a countdown in C# (inside Coroutine) 2 Answers
selective transparency 3 Answers