Variable is not being constant through script execution
I have a variable:
public float number;
which I set in the editor to 0.5.
However, during script execution it periodically being perceived as zero.
There are no actions done to change it and it works fine if I declare it as constant, however, I want to be able to edit it.
I tested it by calling Debug.Log(number) in FixedUpdate and/or Update, but every second time it displays the variable as zero.
What is the issue and how can I solve it?
Could it be possible to announce it as public constant or readonly to be able to edit it in the editor?
Answer by UnityCoach · Apr 28, 2017 at 12:41 AM
You can make it serialised and private.
[SerializeField] float _number;
and if you need to access it from another script, you can add an accessor
public float number {get {return _number;}}
I want to be able to change it from the editor, otherwise I can just make it constant.
Then use this :
[SerializeField] float _number;
public float number {get {return _number;}}
the first allows you to change it in the editor, the second allows you to get the value from other scripts.