- Home /
Are changes of a member reflected in other references to it?
If I take an object value from a class and pass it as the value for a field in a different class, will changes done to that object value be reflected in both classes should either one make changes to it.
Example:
public class SharedClass {
int i;
}
public class A
{
public SharedClass sharedClass = new SharedClass();
void Main ()
{
sharedClass.i = 4;
}
public class B
{
public SharedClass othersharedClass = A.sharedClass;
void Main ()
{
othersharedClass.i = 5;
}
}
}
Answer by sparkzbarca · Apr 11, 2014 at 11:42 PM
pass by value will not change no only pass by reference.
basically changing one value will not automatically change another. The only time your going to get that is pass by reference. Thats because in the case of pass by reference what your really doing is having one value that multiple objects look to for information.
so
A.value = B.value
b changes
A does not change
A.reference = B.value
B changes
A is looking right at B so so the A.reference changes
Your answer
![](https://koobas.hobune.stream/wayback/20220613142521im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
problem with my simple script please help. 2 Answers
Manipulate gameobjects, pick up & rotate 0 Answers
Official Unity Space Shooter Tutorial 1 Answer
Custom Physics? 2 Answers