Quantcast
Channel: Questions in topic: "twice"
Viewing all articles
Browse latest Browse all 70

Code behaves unexpectedly when not in FixedUpdate

$
0
0
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 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.

Viewing all articles
Browse latest Browse all 70

Trending Articles