- Home /
What did unity make the gameobjects always pass by reference when using as variable in functions?
As we know in C#, when we use a variable in a function ,it would always copy the value to the function, so if we change the value in the function, the function would only change the "copy" one instead of changing the original value only if we add a "ref" before the variable. However, in unity, when using gameobject in the function, the function can always change the original gameobject property.
for example:
public GameObject just_a_object;
private void Start()
{
int A = 1;
change_A_1(A);
print("A=" + A);
change_A_2(ref A);
print("A=" + A);
just_a_object.name = "original name";
change_object_name(just_a_object);
print("object name = " + just_a_object.name);
}
void change_A_1(int A)
{
A = 2;
}
void change_A_2(ref int A)
{
A = 3;
}
void change_object_name(GameObject a_object)
{
a_object.name = "another name";
}
and the output would be:
A=1
A=3
object name = another name
what did unity do to make the gameobject class has such kind of feather, says, no matter how we pass it, the function can always find the "original" gameobject in the game?
Answer by rainChu · Aug 19, 2018 at 12:46 AM
This isn't something Unity specifically does. You misunderstand how C# passes by value or reference.
In C#, classes are always passed by reference, and GameObject happens to be a class. Value types, such as int, float, and structs, are always passed by value and require the ref specifier to pass a reference to them to a function.
This is why Vector3 always makes copies (It's a struct) and why GameObject, MonoBehavior, and everything you derive from it, always passes by reference.
If you want to use this behavior yourself, all you have to do is make sure that what you're passing is a class, and C# will handle the rest.
No, No, No. You haven't understood the way how variables are passed either. Variables are always passed by values unless you use "ref" or "out". People just confuse what a variable is and what its "content" is. Classes are reference types. The content of a variable of a reference type is the reference, not the object itself. When you pass a reference type variable to a method, the content of the variable is copied onto the local stack. Any changes to the variable won't affect the variable passed in. However when you dereference the reference value that got copied to access the object behind the reference the whole pass by value / pass by reference is irrelevant.
Changing a variable means to modify the "variable content". That means this:
void SomeNormal$$anonymous$$ethod(GameObject aGO)
{
aGO = new GameObject("NewGo");
}
GameObject myVariable;
SomeNormal$$anonymous$$ethod(myVariable);
Here "myVariable" will not be affected when you change the variable "aGO" inside the method since it was passed by value. However here's the same example with a ref parameter
void SomeRef$$anonymous$$ethod(ref GameObject aGO)
{
aGO = new GameObject("NewGo");
}
GameObject myVariable;
SomeRef$$anonymous$$ethod(ref myVariable);
In this case we actually pass the variable by reference. So "aGO" is just an alias for myVariable inside the method. So assigning new / different "content" to the variable will affect "myVariable" outside the method.
This is what passing by value or passing by reference is about, regardless of what type of variable you pass.
The same thing applies when you "assign" one variable to another. You always copy the content of the variable into the other variable. In the case of reference types the content is the reference.
Thank you for the clarification to my answer. As a C++ programmer I am very familiar with the fact that the reference/pointer itself is a value type. Edit: Yes, I see what you mean. $$anonymous$$y ter$$anonymous$$ology was incorrect.
Your answer

Follow this Question
Related Questions
How do I get a material from a GameObject? 1 Answer
Character animation problem 1 Answer
objects in the same array wont move 0 Answers
Transport objects between scenes. 1 Answer
How you find an Object 1 Answer