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
2
Question by look001 · Sep 01, 2017 at 04:49 PM · referencefloatreferencingundobackup

store a reference to a float in a variable? (similar to what the "ref" keyword does)

Hello everyone,

is it possible to store a referece of a float? What i tried (using the "ref" keyword):

 List<FloatInfo> backups = new List<FloatInfo>();
 public void BackupInt(ref float referenceToFloat, float value) {
      backups.Add(new FloatInfo(referenceToFloat, value);
 } 
 ...
 public class FloatInfo {
     public float reference; // Since the "ref" keyword is only available for function parameters i can't do "public ref float referece";
     public float value;
     public FloatInfo(ref flaot reference, float backupValue) {
          // unfortunately this copies the variable instead of using the reference. Therefore the reference is gone :(
          this.reference = reference; 
          this.value = backupValue;
     }
 }

I need the reference to know what variable i am dealing with. When i need to call a backup i just want to set the int reference equal the backupValue. By that i want to achieve a Undo system.

Hopefully you understand what i want to achieve (It's kind of tricky to explain). Do you have an idea to solve this?

(wrapping the int in a class is not the solution. The reference realy needs to refer to the "origin" float. I want to undo transform.position.x values by that)

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

2 Replies

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

Answer by Glurth · Sep 01, 2017 at 05:20 PM

While there is the nullable float option ( float? ), it does not actually store a reference. Though the ref keyword does exactly what you want, it can only be applied to function parameters, not member variables.

Alas, I don't think this is going to be possible, without a wrapper class. (Hopefully, someone will post otherwise.)

That being said, don't forget you have the ability to define IMPLICIT cast to/from float functions, that should allow you to use your wrapper class everywhere you can use a normal float. But even then, if want to keep a reference, rather than a value, you would need to declare the variables you STORE as that wrapper-class, rather than a float. (This obviously excludes the transform.position.x you wanted to use: in this case, you would need to keep the reference to the transform class itself, and somehow, know which member of the transform to use.)

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 look001 · Sep 01, 2017 at 05:38 PM 0
Share

Thank's a lot! I will try to solve this with a wrapper class :)

avatar image
1

Answer by Jeshira · Nov 01, 2020 at 12:48 PM

I think Unite Austin 2017 conference about game architecture seems to answer this, and spoiler, with ScriptableObject.

 //FloatVariable.cs
 [CreateAssetMenu(fileName ="FloatVariable", menuName = "Architecture/Float Variable")]
 public class FloatVariable : ScriptableObject {
     public float value;
 }

 //FloatReference.cs
 [System.Serializable]
 public class FloatReference {
     public bool useConstant = true;
     public float constant;
     public FloatVariable variable;
 
     public float Value => (useConstant)
              ? m_constant : m_variable.value;
 }

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 · Nov 01, 2020 at 02:37 PM 1
Share

Right, that's what Glurth meant by "wrapper class". I can also highly recommend this Unite talk.

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

70 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

Related Questions

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

How can I change a float value of an instantiated object from another script? 3 Answers

Object reference not set to an instance of... 2 Answers

Weird problem with referencing game manager 1 Answer

I'm having problems with referencing other scripts. 2 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