- Home /
The question is answered, right answer was accepted
Why is my variable updating when it shouldn't?
I have a globally declared private dictionary variable named joint that takes a string and a class A
This gets populated in the Start() function. I use joint later on in a function named SetPosition() which gets called via Update(). And I use joint in this way:
var position = joint[i].transform.localPosition 2;
I was expecting joint to maintain its original localPosition values over time considering how it was populated in the Start() function, but for some reason, when I output it, the localPosition in joint changes over time even though I only use it in the arithmetic. I never assign anything to joint.
There is another variable (modifiedJoint) of the same type and contains the same data as joint, but this is the one I expect to have the changes over time and is populated in SetPosition(). It gets assigned the localPosition changes based on the arithmetic above.
I was intending to use joint as a container for the original transforms, so that when trying to do arithmetic, I still have the original positions of the gameObject. I need this info because I am trying to move a gameObject forward/backward, and I need to return the gameObject to its original position once it moves "backwards". But evidently the joint position values change, and I don't know why they do.
Is doing arithmetic with transform.localPosition enough to change a gameObject's transform automatically without assignment?
A Dictionary of type Dictionary<string,A>
contains references to objects as its Values. If they move, then their positions as accessed through the Dictionary will change (even though the Dictionary itself hasn't).
If you want to just store the initial positions so you've got them to use later, why not just store the initial positions? i.e use a
Dictionary<string, Vector3>
. The positions stored in the Dictionary will then be fixed until you change them explicitly via the Dictionary.
Also, can't think what you mean by "globally declared private variable" - am assu$$anonymous$$g the "globally declared" was a mistake.
"globally declared private variable" means it is global scope in the script and private for protection level.
I'm still confused, sorry. To me, the very idea of something being "global" precludes the possibility of it having a "protection level". What am I missing? Perhaps a bigger code snippet showing the structure of the file/class and where/how the joint
variable is declared would help.
@Bonfire-Boy yes, that is correct. It was the dictionary causing the issue
Answer by VisionElf · Sep 20, 2021 at 01:56 PM
You are registering a reference to the Transform. The transform is changing position, you'll have a different value for transform.localPosition.
You might want to store a Vector3 instead of a Transform if you want a fixed value.
Follow this Question
Related Questions
Need insight on strange (simple) 2D object transform bug 0 Answers
Locating Position of Object not Working? 2 Answers
Best way to detect if an object is near? 1 Answer
[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer
postion scale and rotation greyed out and not working 1 Answer