- Home /
Why is my StartCoroutine within a IEnumerator bombing Unity? C#
The below code bombs Unity without an Error message straight to my OS. The offending line is the StartCoroutine within CheckMaximiseMenu IEnumerator.
Is this issue a lack of Co-routine knowledge or a legitimate bug ?
using UnityEngine;
using System.Collections;
public class GUIManager : MonoBehaviour {
void Start ()
{
StartCoroutine(CheckMaximiseMenu(new Rect(Screen.width - 10, 0, 10, Screen.height)));
}
IEnumerator CheckMaximiseMenu(Rect pRect)
{
while (!pRect.Contains(Input.mousePosition))
yield return 0;
StartCoroutine(CheckMinimiseMenu(new Rect(Screen.width - 50, 0, 50, Screen.height)));
}
IEnumerator CheckMinimiseMenu(Rect pRect)
{
while (!pRect.Contains(Input.mousePosition))
yield return 0;
StartCoroutine(CheckMaximiseMenu(new Rect(Screen.width - 10, 0, 10, Screen.height)));
}
}
Hard to tell. The code you have there is perfectly fine. Coroutines can be started from within coroutines without problem usually. Do you have anything else running in your scene?
There is nothing else in my scene, bar the actual gameobject the said script is attached too. Presumably its a bug ? can anyone replicate ?
Running unity v 4.3.0f4
@$$anonymous$$olodej You are very right. I totally missed that.
@tr3dent The problem would stem from that not all code paths return a value. You need to put a yield return 0 outside the while loop as well, or use a do-while loop ins$$anonymous$$d.
What $$anonymous$$olodej said seems to be the case here. What are you trying to achieve? It's unusual that you would need to start a Coroutine for GUI on Start().
Are you trying to make a GUI element expand out when someone mouses over it? In such a case, you can simply do that in your Update() function, without the Coroutine overhead. Here's an example:
private Rect pRect;
void Update () {
if(pRect.Contains(Input.mousePosition))
{
pRect = new Rect(10,0,90, Screen.height);
}
else
{
pRect = new Rect(10,0,10, Screen.height);
}
}
void OnGUI()
{
if(GUI.Button (pRect,"pRect"))
{
}
}
No you are right, Invertex - That would be the way to do it. I was testing some things out and ran into this issue along the way.
Answer by Kolodej · Jan 10, 2014 at 10:13 AM
Hi, if I am not wrong, there is a infinite loop of calls between CheckMaximiseMenu and CheckMinimiseMenu. If the mouse position is at "Maximise" coords, then is also the "Minimise" condition fulfilled.
Thanks kolodej, Was a logic error, I can't mark this as answered :(
Good thing its home time soon :).