This is an IEnumerator
that you’re trying to call. To execute it, you need to use StartCoroutine - Like StartCoroutine(EnterState_PlayerSelectingTarget());
And, not that it’s related, but using methods like EnterState_PlayerSelectingTarget
isn’t really elegant. - What would be nicer is that if you used an enum to represent your states, and then have one method EnterState(State state)
that acts accordingly upon the argument state
.
And don’t forget that there is more than one stepping method in debugging, there is step in
, step out
and step over
- If you’re stepping over your method, you will never get inside it, so you need to step in
- More on debugging with monodev.
EDIT: About your waiting mechanism, I used to actually fall for this myself
Here’s what you’re doing:
// some code
StartCoroutine(WaitForPlayer());
// some other code (which you intend for it to be executed AFTER you've waited for the player)
Where, WaitForPlayer
:
IEnumerator WaitForPlayer()
{
while (targetHex == null)
yield return null;
}
To understand the problem, you have to understand how yield return
works - yield return null;
means skip what’s after the yield to the next frame and RESUME what’s after the Coroutine - it does NOT mean skip what’s after the yield AND what’s after the Coroutine to the next frame - yield return new WaitForSeconds(1);
means, wait 1 second, and then execute what’s after the yield. In your case, there is ‘nothing’ after yield return null
- so even if targetHex
were to to assigned (not null), there’s nothing that you’re doing after the yield inside your Coroutine
Here’s how execution will flow:
1 -> // some code
2 -> StartCoroutine(WaitForPlayer());
3 -> while (targetHex == null)
4 -> yield return null;
5 -> // some other code (which you intend for it to be executed AFTER you've waited for the
To fix this, there’s a couple ways:
1- Don’t wait inside a Coroutine, wait like this:
// some code here
while (targetHex == null)
yield return null;
// resume :)
2- Use a isPlayerReady
(or something) flag and stick to your waiting idea of using a Coroutine, like this:
isPlayerReady = false;
// some code here
StartCoroutine(WaitForPlayer());
Where, WaitForPlayer
:
IEnumerator WaitForPlayer
{
while (targetHex == null)
yield return null;
isPlayerReady = true;
}
And then somewhere else, like in Update
:
if (isPlayerReady)
{
isPlayerReady = false;
// resume :)
}
3- Better yet, use events - Have an event like, OnPlayerReady
that gets ‘fired’ when you’ve assigned/populated all your stuff and you’re ready to go - inside, put whatever code you want, and let whatever interested in that event ‘subscribe’ to it.
More about events could be found here, here, here and everywhere googling “C# events”. Events are very powerful and will make your coding a lot more pleasant!