Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hong-En-Tsai · Aug 19, 2018 at 12:40 AM · gameobjects

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 19, 2018 at 08:36 AM 0
Share

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.

avatar image rainChu Bunny83 · Aug 19, 2018 at 08:39 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges