- Home /
Code behaves unexpectedly when not in FixedUpdate
Hi, my problem is that part of my code behaves in a strange way sometimes, not as logically would expect from reading the code. After a bit of messing with the code I tried using a fixed update instead of a normal one and this seems to solve the problem. I would like to know where the problem is, can you help me understand why this happens?
This is part of the code, i'm almost certain the rest is not relevant cause i made some tests.
class Logic:
public static bool isActive = false;
private OrderedList<Character> inTurn;
public static GameObject entityTurn;
void Update () {
while (!isActive) {
if (inTurn.count > 0) {
isActive = true;
Character c = inTurn.removeFirst ();
entityTurn = c.gameObject;
c.chooseAction ();
} else {
//NOT RELEVANT CODE
}
}
}
class Character:
private bool isSelecting = false;
void Update () {
if (isSelecting) {
if (Input.GetKeyDown (KeyCode.Alpha8)) {
isSelecting = false;
turn = 40;
hasMoved = false;
hasAttacked = false;
Logic.isActive = false;
}
}
}
public void chooseAction () {
if (!hasMoved || !hasAttacked)
isSelecting = true;
else {
turn = 0;
hasMoved = false;
hasAttacked = false;
//Logic.isActive = false;
}
}
There is only one gameobject that has a Logic script attached and multiple gameobjects that have the Character script attached.
The problem is that sometime, even if isActive (in Logic) is true(so, there is an active selected character), the program enters the loop again and enables isSelecting for another characters and when i press the input key it takes the input two times, skipping one of the characters taken from the inTurn list. The only thing that can make isActive true is the end of the input if, so again the input must be taken twice to make the loop enter the second time(when it shouldn't). This makes no sense to me one condition that causes the problem is activated only if the other is activated and vice versa.
As I said using a FixedUpdate in the Logic class seems to solve the problem but i don't understand why.
Sorry if my description is not the best but I'm not really sure what the problem is.
Answer by Bunny83 · Aug 12, 2014 at 11:02 AM
Well, are you sure that in your character script line 23 is commented out? If not it could use up all your characters at once since you use a while loop in your "logic" script.
Also your condition inside chooseAction looks a bit strange to me. You always set isSelecting to true except when hasMoved and hasAttacked are true.
It would make more sense to put the input check into your logic script to have it at one place. Same for your isSelecting. It's easier to use a variable of type "Character". If it's null nothing is seleced otherwise it holds the one Character you've selected.
From those code snippets it's hard to determine the actual use of those two scripts. Some more background would help i guess.
Just to be clear: FixedUpdate runs right before Update. However depending on your framerate it might be skipped or even run several times per Update. So you might have a 1-frame dependency between those two scripts based on the order in which they are run.
i'm sure that that line is commented
that is what i wanted but to find out the problem i commented most of the code and the method never goes in the else part
yeah, i thought of rewriting this part to make it better but i'm also curious to find out what the problem is in the current version before making any change
well, as i said i commented out most of the code and the program can't do anything else other than those two scripts, but for more information this is what it should do:
there are 11 characters every one with his speed increasing a variable turn, when turn is 100 the character enters the inTurn list. If inTurn is not empty i pick up and remove the first item and i activate it's "isSelecting" so he can choose if attack/move/wait (in the code i commented all except wait because the problem occurs mostly there). After i choose to wait with the keyboard(in the current situation is the only thing i can do) it's turn variable is set to 40 and put isActive= false so that the program knows that can choose another character from the list.
That's it the other code is either commented or it's never called so it's unreacheable.