Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by gameplay4all · Oct 10, 2014 at 12:09 PM · variablesclasslinking

How to 'link' variables?

Hi,

I would like to know something that I need for a script in my game. Let's say I have a Class:

 public class ExampleClass{
 var string1 : String;
 var int1 : int;
 var boolean1 : boolean;
 
 }

And now if I would setup two different objects, one with a Script called ClassHolder which holds just one variable of the above described ExampleClass that is called exampleClassOnClassHolder. And one that holds this script:

 var currentlyConnectedClass : ExampleClass;
 
 function AssignConnectedClass( obj : Transform){
 currentlyConnectedClass = obj.GetComponent(ClassHolder).exampleClassOnClassHolder;
 }
 
 
 function AdjustSomeVariables(){
 currentlyConnectedClass.string1 = "Test";
 currentlyConnectedClass.int1++;
 currentlyConnectedClass.boolean1 != currentlyConnectedClass.boolean1;
 }


If I would call the first function, would that mean that currentlyConnectedClass will just copy the other object's ExampleClass variable, or completely link it? What I mean with 'link' is that if I would call AdjustSomeVariables(); the variable on this script would change as would the one on the ClassHolder script. I believe this is not the case, so how could I 'link' variables as described as above? Or a method to achieve the same result:)

I know this is a really vague question but I tried to explain it as best as I could! Thank you in advance!

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
4
Best Answer

Answer by NoseKills · Oct 10, 2014 at 07:55 PM

If i understand your question right, the short answer is: it does get "linked".

You should read up on "reference types" and "value types". It's a very basic concept in object oriented programming and without understanding it you'll run into problems all the time.

In short, variable types like int, float, char (and all Structs: Vector2, Vector3) etc. are value types. Any time you assign a value to a value type variable or pass one as a parameter to a function, it gets "copied" => changing the original variable doesn't change the newly assigned one.

Reference type variables point to an object in memory. When you assign or pass them around, you still have just one object in the memory, but multiple variables referencing it. This is the case in your example.

 // VALUE TYPES
 var a:int = 1;
 var b:int = a; // int is value type so the value of a gets copied into b
 a = 2; // b is still 1 after this. a and b are not "linked" (they are not pointing to the same object in memory)
 
 var a:float = 1;
 changeFloat(a)
 ...
 void changeFloat(parameter:float)
 {
     parameter = 2; // <- value type
     // the variable a outside of the function is still 1
     // because float is a value type, "parameter" and "a" are not "linked" 
 }
 
 // REFERENCE TYPES
 var a:GameObject = new GameObject("carrot");
 var b:GameObject = new GameObject("apple");
 a = b;
 // now both variables a and b are pointing to the same object (b / "apple")
 // we have lost "carrot" because no variable is pointing to it
 
 a.name = "peanut";
 Debug.Log(a.name);
 Debug.Log(b.name);
 // prints out "peanut" twice, since both variables are just references to the 2nd created GameObject 
 
 var a:GameObject = new GameObject("horse");
 a.transform.position = Vector3.zero;
 
 void moveHorse(parameter:GameObject)
 {
     parameter.transform.position = Vector3.one;
     // the original "horse" we created outside the function is moving
     // because "parameter" and "a" are just references to the same object
 }


Anything you declare a class (like ExampleClass) is a reference type, so you are passing around a reference to it. You have just one of that object in the memory and when you do AdjustSomeValues(), you adjust the values of that specific object no matter shich variable you use to do so.

Comment
Add comment · Show 1 · 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 gameplay4all · Oct 20, 2014 at 09:06 PM 1
Share

Hi, thank you for your perfect answer! I read it some time ago but I got distracted I think because I didn't mark it as an answer :/ sorry!

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

29 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

Related Questions

Class Scope Variable 1 Answer

How do I save an array of all different variables? 0 Answers

How to use if statements for inspector in class? 1 Answer

What is the best way to store data about a GameObject? 1 Answer

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 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