Reputation: 33
I was trying to initialize a public variable, a Vector3, at the start of a class using its default constructor. The line of code itself looks like this:
public Vector3 DefaultOffset = new Vector3(0.0f, 2.0f, -10.0f);
Now everything runs fine from this ... but it ignores the negative sign on that last number, and instead the variable "DefaultOffset" is initialized as if the code didn't have it, i.e.:
public Vector3 DefaultOffset = new Vector3(0.0f, 2.0f, 10.0f);
Now it isn't anything further down in the code taking an absolute value, I checked. DefaultOffset is never assigned another value anywhere in all my code, it could even be made into a protected constant (at the moment, later its value will likely change during runtime, but no such code exists).
After initializing DefaultOffset I can assign the value -10.0f to its "z" value during run time, and that stays and works just fine. Playing around with printing variable values, it really does seem that in this assignment the " - " is just completely ignored during assignment, its like assignment applies the ABS function to all the values.
Out of desperation I tried using both the " - " and the " – " sign. Obviously the latter one (" – ") just throws an error as it is an unrecognized token, but the appropriate former one (" - ") is just ignored, like it isn't even there or as if I had commented it out. Indeed, look at this picture of the code in the actual IDE and see how it is colored. Notice that the IDE turns recognized numerical values orange including the qualifying characters, like the decimal point and the "f" noting the float value at the end ... but the " - " sign has not been turned orange here like it normally would. It is as if the " - " were not recognized as part of the number at all by the IDE. (Yes I know, the color coding in the IDE is separate from the compiler itself and being completely independent of one another it is possible for the two to actually have completely different behavior on recognizing tokens and values and such, but both seem to have the same interpretation of my "-10.0f" here as just "10.0f".)
Am I doing something stupid and I need to add a little bit more syntax or qualifiers for this to be recognized as a negative number? ... or have I actually run into an error in the IDE and/or compiler? "Why not just change assignment at runtime then? you already said this works!" Well that is what I am doing currently, but I am a nit-picky pedant with OCD-like qualities and given that this assignment should work it is driving me insane that I can't get the proper assignment to a negative number this way.
Any thoughts help?
Upvotes: 1
Views: 343
Reputation: 8163
public
variables have a tendency to do that in Unity. Once you set it to something in the inspector, it doesn't matter what you change it to in the code, it takes the value it used to be from the very very first time you set it. So basically you'd have to either change it in the inspector, or make it protected
, then change it, then make it public
again.
EDIT: or you can make it a field:
public Vector3 DefaultOffset
{
get { return new Vector3(0.0f, 2.0f, -10.0f); }
}
or it should even work if you set the value after Awake()
public Vector3 DefaultOffset;
void Awake() // Or Start or OnEnable or Update or FixedUpdate.....
{
DefaultOffset = new Vector3(0.0f, 2.0f, -10.0f);
}
Upvotes: 0
Reputation: 869
Have you tried doing 0.0f - 10.0f
instead of just -10.0f
? If you break it down, that's really what you're asking the C# compiler to do.
Upvotes: 1