object assignment confusion

i just made a PlayerSpell class and two variables of it A and B.
i wrote:

B = A;

in the Update() just as a check to see how they work, and it turns out that I’m able to change B and all the changes are updated into A. i thought info was supposed to come over the ‘=’ from left and go into the right side? so it would basically read B is equal to A.
but it seems to be the opposite. A is equal to B… am i doing something wrong or is this the way it goes in unity script? or does the fact that they are classes have something to do with it?

1 Answer

1

Variables that refer to classes are by reference, so when you assign A = B, A and B will point to the same class. Any changes made to either A or B will be reflected in both since they both refer to the same class.

Unity also contains structs (structures). Vector3 and Color are both structures. When you read the reference, you will see the word ‘struct’ below the name for structures. Structures are copied by value. So if A was a structure, your ‘B = A’ would assign B a copy of A. Any changes in B would not be reflected in A.

ok, that makes sense thank you.

do you think you could give me a quick javascript example of a struct? i tried looking around the internets, but i couldn't figure out the syntax. (if you know javascript that is)

@hardmode: You may be interested in reading this post: [Struct in JavaScript][1] [1]: http://answers.unity3d.com/questions/23044/struct-in-javascript.html

@hardmode - good reference. JavaScript makes it a bit more difficult.

ok thanks, ill look into that!