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 /
This question was closed Sep 15, 2016 at 09:54 AM by SethSlax for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SethSlax · Sep 14, 2016 at 12:33 PM · variablesreference

Adding a permanent reference to a variable. Is it possible?

Hey everyone.

So I know it's possible to reference just about every type of class/object by either assigning it in the inspector or grabbing it via GetComponent, but is it possible to have permanent references to the more basic variable types? (i.e. int, float)

Say I have a class that tracks all kinds of data, and I want to add a script that looks for changes in a specific int or float variable of another class, is there a way to assign a reference to an int or float? Assigning it via myFloat = anotherFloat simply assigns it the value during that frame, and I'd rather not run that in Update().

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

4 Replies

  • Sort: 
avatar image
2

Answer by Jessespike · Sep 14, 2016 at 05:11 PM

Not possible. Float cannot reference another float.

Classes are Reference-Type. Primitives like int and float are Value-Types.

One common solution is to write a CopyTo function, which copies all of the values from one reference to another.

Comment
Add comment · 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
2

Answer by SterlingSoftworks · Sep 14, 2016 at 05:23 PM

You could try using events if you don't want it to be in Update?

 public delegate void ValueChangedEvent(float newFloat);
 
 public static event ValueChangedEvent OnFloatChanged;

Then where ever in your code you are changing this float value, put in

 if(OnFloatChanged != null)
     OnFloatChanged(changedFloat);

Now, where you're tracking it, you put in

 void OnEnable(){
     <ScriptWhereOriginalFloatIs>.OnFloatChanged += FloatChanged;
 }
 
 //and
 
 void OnDisable(){
     <ScriptWhereOriginalFloatIs>.OnFloatChanged -= FloatChanged;
 }

Then you'll need a function CALLED "FloatChanged".

 void FloatChanged(float newFloat){
     trackingFloat = newFloat;
 }

The way events work is that, when the original "OnFloatChanged" is called, every script that's "listening" for it (listeners are added/removed in the OnEnable and OnDisable lines) gets that value originally sent.

It's like hearing a siren on the road, if you're listening for it, you drive to the right of the road to get out of the way. If you aren't listening for it, you don't do anything.

Unless somebody has a better solution besides using Update, this is all I can think of.

Comment
Add comment · 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
1

Answer by JoshDangIt · Sep 14, 2016 at 05:26 PM

Not sure if it's what you're looking for, but you can use properties.

 public int number
     {
         get{ return otherObj.otherNum;}
     }


https://unity3d.com/learn/tutorials/topics/scripting/properties

Comment
Add comment · 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
1

Answer by Bunny83 · Sep 14, 2016 at 05:30 PM

Another way would be to use something like a "proxy" which might use two closures to actually get / set the variable. However that's a quite unusual case. You would need a good reason to do something like that.

 public class VariableProxy<T>
 {
     System.Func<T> m_Get;
     System.Action<T> m_Set;
     public VariableProxy(System.Func<T> aGetter, System.Action<T> aSetter)
     {
         m_Get = aGetter;
         m_Set = aSetter;
     }
     public T Value
     {
         get { return m_Get(); }
         set { m_Set(value); }
     }
 }

So here you can create an instance of that class and assign two methods to it which actually read and write the value. This allows you to access a single variable in a generic way.

Say you have a script instance "obj" that has a public float variable named "myFloat". You can do this:

 VariableProxy<float> myProxy = new VariableProxy<float>(()=>obj.myFloat, (v)=>obj.myFloat = v);

This class instance "myProxy" can now be used to read and write the myFloat variable

 float v = myProxy.Value; // read
 myProxy.Value = 5f; // write

Since myProxy is a class you can pass it's reference around and store it whereever you want. So if you have different classes with different float variables you can create a proxy for each and put those proxies in an array / List. Now you can access them in a generic way. How you organise or differentiate them is of course up to you ^^.

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 Bunny83 · Sep 14, 2016 at 05:42 PM 0
Share

This is basically similar to a closure (well it actually uses two closures ^^). However it can be used without creating a closure by creating getter and setter methods inside the target class.

 public class SomeClass
 {
     VariableProxy<float> m_$$anonymous$$yFloatProxy = null;
     public float myFloat;
     public VariableProxy<float> $$anonymous$$yFloatProxy
     {
         get
         {
             if (m_$$anonymous$$yFloatProxy == null)
                 m_$$anonymous$$yFloatProxy = new VariableProxy<float>(Get$$anonymous$$yFloat, Set$$anonymous$$yFloat);
             return m_$$anonymous$$yFloatProxy;
         }
     }
     float Get$$anonymous$$yFloat()
     {
         return myFloat;
     }
     void Set$$anonymous$$yFloat(float aValue)
     {
         myFloat = aValue;
     }
 }

This way you don't need seperate closures for each delegate. Here the "class itself" is the closure. When you read "$$anonymous$$yFloatProxy" for the first time it creates an instance and reuses it if you read it again.

This looks more complicated but actually is better for performance / memory footprint.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing class data with a reference? 2 Answers

Problem with accessing static variables 1 Answer

Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer

how to reference a javascript variables in a c# code. 0 Answers

How to change a value of a reference object variable after a while ? 0 Answers


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