C
C#2y ago
EmoEmu_69

Having trouble getting my coroutine to start

Any help is appreciated, thanks in advance!
69 Replies
EmoEmu_69
EmoEmu_692y ago
for reference, I've been testing it with the difficulty set to 1
hiyosilver
hiyosilver2y ago
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?
huh.how
huh.how2y ago
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
EmoEmu_69
EmoEmu_692y ago
yeah
hiyosilver
hiyosilver2y ago
Start actually runs too? I'm no Unity expert, but as far as I can tell this should work.
EmoEmu_69
EmoEmu_692y ago
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
hiyosilver
hiyosilver2y ago
Well if you have no error highlighting that is probably the first thing to fix Hmm
EmoEmu_69
EmoEmu_692y ago
yeah I just fixed that after vertx mentioned it, there's no errors
huh.how
huh.how2y ago
you should check what isGameActive is at the top of SpawnMystery, outside of the while loop
EmoEmu_69
EmoEmu_692y ago
you mean like see if it's registering correctly?
huh.how
huh.how2y ago
You're making a lot of assumptions, check anything that could be wrong preferably using the debugger
hiyosilver
hiyosilver2y ago
If it's true that the Coroutine is never started, the problem must be earlier.
huh.how
huh.how2y ago
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
hiyosilver
hiyosilver2y ago
Ok, yeah that's actually a decent possibility 😄
huh.how
huh.how2y ago
if it never runs at all, then this script was never added to an object in the scene
EmoEmu_69
EmoEmu_692y ago
I checked out of the while loop
hiyosilver
hiyosilver2y ago
@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.
EmoEmu_69
EmoEmu_692y ago
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
huh.how
huh.how2y ago
perhaps you disabled the script accidentally
EmoEmu_69
EmoEmu_692y ago
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
hiyosilver
hiyosilver2y ago
So, does that mean the issue is fixed or where are we at?
EmoEmu_69
EmoEmu_692y ago
basically we know for sure that the problem is the while loop
hiyosilver
hiyosilver2y ago
Meaning the Coroutine now definitely starts but nothing in the while loop executes?
EmoEmu_69
EmoEmu_692y ago
yeah
huh.how
huh.how2y ago
hence why I asked
hiyosilver
hiyosilver2y ago
well then isGameActive must not be true when you think it should be.
EmoEmu_69
EmoEmu_692y ago
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
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
yes
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
ah that makes a lot of sense
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
bro you're a genius
hiyosilver
hiyosilver2y ago
Nah, not even close. But that sounds like the issue might be resolved? If not, feel free to ask more questions 😉
EmoEmu_69
EmoEmu_692y ago
doesn't seem like that solved the issue, but it's certainly a step in the right direction
hiyosilver
hiyosilver2y ago
Is it returning false still?
EmoEmu_69
EmoEmu_692y ago
I'm not quite sure how I would test that
hiyosilver
hiyosilver2y ago
Well you said the issue is not resolved, so I would assume the while loop is still not running?
EmoEmu_69
EmoEmu_692y ago
sorry it's late and I have a case of dodo brain, lemme test that rq 💀
hiyosilver
hiyosilver2y ago
Happens to the best of us.
EmoEmu_69
EmoEmu_692y ago
okay it looks like the thing is not running
hiyosilver
hiyosilver2y ago
Just the while loop or the entire coroutine again?
EmoEmu_69
EmoEmu_692y ago
the loop
hiyosilver
hiyosilver2y ago
Ok if you Log the current value of gameMaster.isGameRunning at the top of the coroutine outside the loop. It should print false correct?
EmoEmu_69
EmoEmu_692y ago
so it's starting as false and not updating after I hit the start game button
hiyosilver
hiyosilver2y ago
Well presumably by that time its too late At the point where you call the Coroutine, isGameStart is not true yet.
EmoEmu_69
EmoEmu_692y ago
right
hiyosilver
hiyosilver2y ago
So the coroutine hits the while condition and goes "oh well I'm not supposed to do this" And the coroutine ends
EmoEmu_69
EmoEmu_692y ago
aaaaahhhh that makes sense
hiyosilver
hiyosilver2y ago
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?
EmoEmu_69
EmoEmu_692y ago
is there a way to start a coroutine from another script? wdym?
hiyosilver
hiyosilver2y ago
Could you not just put this spawning logic in the update function? Oh wait no, you need the specific seconds nvmd Sorrry 😄
EmoEmu_69
EmoEmu_692y ago
nah you good lol
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
wouldn't that get kinda laggy?
hiyosilver
hiyosilver2y ago
Well it's kinda ugly too. It's really early here so my brain isn't working too well either 😛
EmoEmu_69
EmoEmu_692y ago
xD yin and yang
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
ah gotcha
hiyosilver
hiyosilver2y ago
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 :/
EmoEmu_69
EmoEmu_692y ago
testing it now
EmoEmu_69
EmoEmu_692y ago
it's waiting 10 seconds then spawning an endless wave of mystery boxes
hiyosilver
hiyosilver2y ago
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.
EmoEmu_69
EmoEmu_692y ago
I didn't really understand most of that Imma take a shower real quick and refresh my braincells
hiyosilver
hiyosilver2y ago
Do that I'll write up an example, even if I'm not sure its right 😄
EmoEmu_69
EmoEmu_692y ago
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 😄
hiyosilver
hiyosilver2y ago
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.
hiyosilver
hiyosilver2y ago
No idea if this works at all but that would be my best attempt 😄
EmoEmu_69
EmoEmu_692y ago
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!