GetKeyDown running twice
I have a system of turns, where each player has a turn that gets switched via an EndTurn function. This function runs in a GetKeyDown if statement. This function is in multiple scripts, but it shouldn't be possible for those to run at once. Here's why:
if (Input.GetKeyDown(KeyCode.Return) && isTurn)
EndTurn();
isTurn is only true on one Game Object at a time.
It's a little complicated to get into for people who haven't worked on the code, but basically I know for a fact that the turns are switching correctly if I make it impossible for the end turn function to fire on the turn that keeps getting skipped (the second one). Basically, it immediately ends the next turn when the first one ends.
I'll clarify anything I need to on request.
I know this is kind of a weird question, but I'd appreciate the help if you can give it. Thanks!
Answer by xxmariofer · Aug 06, 2020 at 08:42 AM
getkeydown cant be getting called twice, but most likely your issue is with how unity handles its loop, in every frame all the updates of all the objects get called, one by one, lets suppose you have 3 gamobjects Object A,B;C and only B is marked as isTurn true, what unity will do is:
call update on A isTurn is false so skips
call update on B isTurn is true so enters //////AND HERE IS WHERE I HINK YOUR PROBLEM IS
I imagine that in the EndTurn method you will say something like object B turn is false now object C turn is true
Call update on C isTurn is true so enters
it is imposible for me to say without your code, but here is what ithink your issue is
Thanks for your answer, I can only assume this is the case. I can't figure out a way to get this to work, but I found a workaround that's working fine. Thank you.
A better approach wpuld be to have one round manager with only its update a d call the method of the objects turn