- Home /
can't read updated variable
I need to stop/resume the rotation of a fan when a certain key is pressed, so I wrote this C# code:
public bool rotationFlag = false;
private float rotationAngle = 2.0f;
void Update(){
if (Input.GetKey("right"))
roationAngle+=1;
if (Input.GetKey("left"))
roationAngle-=1;
if (Input.GetKeyDown("Keypad0"))
commuteFan();
if(rotationFlag){
fan.transform.Rotate(Vector3.up, rotationAngle);
}
}
public void commuteFan(){
rotationFlag = !rotationFlag;
}
cummuteFan() method is called when I press the button.
the variable starts on false and the fan correctly doesn't ratate, when I hit the button, the method is called, the variable values changes and the starts to rotate, but if I want to stop it, it doesn't work anymore. debug tells me that the value is correctly changed to false in commuteFan() but update() continues to read the old value (true) and the fan doesn't stop...
any suggestion
Can you include all the code please, especially the part where you are checking for the button input.
please check updated question. I think it's a pretty standard code
Answer by jack_the_beast · Sep 28, 2014 at 03:06 PM
[SOLVED] I don't know why, but I created a class with only the bool along with getter/setter methods in it and it works know
Answer by mattyman174 · Sep 28, 2014 at 11:58 AM
if (Input.GetKeyDown(KeyCode.Keypad0))
Use the KeyCode enum to define what keys you want for your input instead of trying to define them by name and see if that works instead.
I think you didn't got the problem the key press works, it correctly changes the flag value, but the new value it's non readed by Update()
The code you have provided works perfectly fine for me.
Just a note, you did miss an opening brace after the following line in the code you provided.
if(rotationFlag)
As far as what you have provided everything works as intended for me. Can you copy and paste the entire class please.
this is the entire class. i only have another couple of lines to increase the rotation angle. I've included them anyway
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to rotate a projectile to face the player in 2D? 1 Answer
Character Not Rotating 0 Answers