Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 KateHassan · Jul 24, 2018 at 09:43 AM · update functiondeltachange variable

Finding change in variable between updates

I would like to work out the change in a variable from the last update. so something like x(i+1) - x(i) but in a c# way. I am pretty new to c# so any help would be great! I can't use the built in mouse position and getaxisraw etc because the x and y data is not coming from the mouse. Hope this makes sense to someone!

Comment
Add comment · Show 1
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 Harinezumi · Jul 24, 2018 at 12:46 PM 0
Share

you mean like storing the previous value in a class variable?

 public class Difference : $$anonymous$$onoBehaviour { 
 
     private float previousValue = 0;
 
     private void Start () {
         previousValue = ... // set it to wherever you get the value
     }
 
     private void Update () {
         currentValue = ... // wherever it comes from - you didn't share this info
         float difference = currentValue - previousValue;
         previousValue = currentValue; // so that you can compare the next time Update() is called
     }
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheKnightsofUnity · Jul 24, 2018 at 12:06 PM

Hello @KateHassan,

The thing you are looking for is properties and delegates.

 public event Action<int> OnVariableChanged = delegate { };
 
 public int X 
 {
     get
     {
         return x;
     }
     set
     {
         int oldValue = x;
         int newValue = value;
         //Here you can find out difference

         x = value;
         OnVariableChanged.Invoke(value);
     }
 }


This way you can keep on track when a variable, for example, is changing in the set block and in the more elegant way using delegates like:

 private void Awake()
 {
     OnVariableChanged += OnVariableChangeHandler;
 }

 private void OnVariableChangeHandler(int value)
 {
     //Do something when variable change
 }

Please feel free to ask any questions you might have.

All the best, The Knights of Unity

Comment
Add comment · Show 6 · 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 KateHassan · Jul 24, 2018 at 12:47 PM 0
Share

Hello, thank you for your reply. I still don't really understand how this gives me the change in x? At the moment I have: public float eyex, eyey, eyeconfidence; public void ReceiveData(float x, float y, float confidence) { eyex = x; eyey = y; eyeconfidence = confidence;

Vector2 ComputeDisplacement() {

    Vector2 retvalue = new Vector2(deltaeyex, deltaeyex); 
     return retvalue;
 }

and I am trying to find deltaeyex

avatar image Harinezumi KateHassan · Jul 24, 2018 at 01:07 PM 0
Share

I think the above solution might be more complex than what you would like to do. Basically it will call the function you specify in the delegate whenever the value changes.

avatar image TheKnightsofUnity KateHassan · Jul 24, 2018 at 01:51 PM 0
Share

If you are changing the value in Update() method, you can write something like this:

 private void Update()
 {
    float oldEyeX = eyeX; //store actual value of eyeX variable
 
    eyeX = 7; //$$anonymous$$odify eyeX value
 
    float deltaX = eyeX - oldEyeX; 
 }
avatar image KateHassan TheKnightsofUnity · Jul 25, 2018 at 12:20 PM 0
Share

Hi, apologies I am still confused! I don't know what I put in for eyeX = 7 as I don't know the eyeX value until the game is running. Does this make sense? I need the difference between eyex in frame 1 and eyex in frame 2 etc..?? $$anonymous$$ate

Show more comments

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

88 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

Related Questions

Update function don't update 1 Answer

Character AI - Are Coroutines less heavy than using functions kept in update 2 Answers

Why Instantiated prefabs in continuous position sequence are overlapping on update?? 1 Answer

why do these both codes work similarly when they are put inside update function... 2 Answers

How to hide a text and show another when Space bar is clicked?,How to hide text after pressing space? 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