Having trouble getting my coroutine to start
Any help is appreciated, thanks in advance!
69 Replies
for reference, I've been testing it with the difficulty set to 1
Does 'trouble' mean you have confirmed that it doesn't start at all? Either by debugging with a breakpoint or writing something to the console?
You first need to properly configure VS.
It should not say Miscellaneous Files in the top left, you have no autcomplete or error highlighting
https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/getting-started-with-visual-studio-tools-for-unity?pivots=windows#install-unity-support-for-visual-studio
yeah
Start actually runs too? I'm no Unity expert, but as far as I can tell this should work.
ah thanks. I've gone ahead and fixed that up
that I haven't tested, lemme give that a shot real quick
yeah it starts
Well if you have no error highlighting that is probably the first thing to fix
yeah I just fixed that after vertx mentioned it, there's no errors
you should check what
isGameActive
is at the top of SpawnMystery
, outside of the while loopyou mean like see if it's registering correctly?
You're making a lot of assumptions, check anything that could be wrong
preferably using the debugger
If it's true that the Coroutine is never started, the problem must be earlier.
if you're unfamiliar with the debugger, there's no time better to learn it than the present https://www.youtube.com/playlist?list=PLReL099Y5nRdW8KEd59B5KkGeqWFao34n
I doubt they ever checked outside of the while loop
Ok, yeah that's actually a decent possibility 😄
if it never runs at all, then this script was never added to an object in the scene
I checked out of the while loop
@vertx Yeah that was my next thought, hence asking to check if Start even gets calld in the first place.
Since the syntax seems right to me.
it's definitely on an object in the scene
gonna try removing it and readding it rq
ok for whatever reason that seemed to fix the issue of the method not being called
perhaps you disabled the script accidentally
maybe
though the script was running. I tested a debug.log in the start() and that went through even before I tried removing and readding the script
So, does that mean the issue is fixed or where are we at?
basically we know for sure that the problem is the while loop
Meaning the Coroutine now definitely starts but nothing in the while loop executes?
yeah
hence why I asked
well then
isGameActive
must not be true when you think it should be.Imma try something rq to make certain of that
aight looks like that was the issue
only problem is that I can't seem to figure out the issue there XD
it should be calling the public bool in my other script
Well, you only initialize it once at the start. I'm only guessing, but is it meant to always have the current value of
IsGameRunning
?
That seems the most logical to me, without knowing the rest of your code. That you intend this to do its things, as long as the IsGameRunning
is true.yes
The way you have it set up right now you only ever get the value once at start and then never change it again.
Maybe the Coroutine should poll the current value itself, rather than relying on that class field.
ah that makes a lot of sense
As a very simple first step, if you just made the condition of the while loop into
gameMaster.isGameRunning
you would always check the current value.
So the coroutine just runs in the background from the start, but only executes it's main functionality at times when this returns true.bro you're a genius
Nah, not even close.
But that sounds like the issue might be resolved? If not, feel free to ask more questions 😉
doesn't seem like that solved the issue, but it's certainly a step in the right direction
Is it returning false still?
I'm not quite sure how I would test that
Well you said the issue is not resolved, so I would assume the while loop is still not running?
sorry it's late and I have a case of dodo brain, lemme test that rq 💀
Happens to the best of us.
okay it looks like the thing is not running
Just the while loop or the entire coroutine again?
the loop
Ok if you Log the current value of gameMaster.isGameRunning at the top of the coroutine outside the loop.
It should print
false
correct?so it's starting as false and not updating after I hit the start game button
Well presumably by that time its too late
At the point where you call the Coroutine, isGameStart is not true yet.
right
So the coroutine hits the while condition and goes "oh well I'm not supposed to do this"
And the coroutine ends
aaaaahhhh that makes sense
Since you only ever call it once in Start, it never gets wind of a change in the game state.
Presumably because that happens when you press the start button as you say.
But in this case, do you even need the coroutine?
is there a way to start a coroutine from another script?
wdym?
Could you not just put this spawning logic in the update function?
Oh wait no, you need the specific seconds nvmd
Sorrry 😄
nah you good lol
Mmh let me think for a second.
Well here's an idea
Don't just start the coroutine from the Start function.
Check the current value of isGameActive in Update
If it is, start the coroutine, if not, stop it.
wouldn't that get kinda laggy?
Well it's kinda ugly too.
It's really early here so my brain isn't working too well either 😛
xD
yin and yang
If you just made it a
while(true)
it would continuously spawn in the background with the right interval.
If you then had a simple boolean field like mysterySpawningActive
or something.
Check in Update if isGameRunning
is true.
if it is AND if mysterySpawningActive
is not, start the coroutine.
If mysterySpawningActive
is already true do nothing.ah gotcha
if isGameRunning is false AND
mysterySpawningActive
is true, stop the coroutine.
And remember to set mysterySpawningActive
whenver you start or stop the coroutine.
Maybe something like that?
I kind of just pulled that out of my backside, so use with caution :/testing it now
it's waiting 10 seconds then spawning an endless wave of mystery boxes
I'm not sure you can use StopCoroutine that way
can you try keeping a reference to the coroutine at the top?
Like
IEnumerator spawnMysteryCoroutine = SpawnMystery();
And the explicitly call that with start and stop
Also, make the while loop condition just true
Use isMysteryActive
in the if checks in Update
To see if the coroutine even needs to be started or stopped at all.I didn't really understand most of that
Imma take a shower real quick and refresh my braincells
Do that
I'll write up an example, even if I'm not sure its right 😄
if you're not here when I get back though, thank you so much for everything; it may not be totally fixed yet, but I couldn't have gotten it this far without your help 😄
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
No idea if this works at all but that would be my best attempt 😄
it works, thanks!
I do have one more issue though, and that's trying to get the coroutine to repeat
nvm I fixed it
thanks again!