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
0
Question by schwertfisch · Dec 18, 2010 at 11:20 AM · timechangevaluevar

Time elapsed between variable change?

Everytime my character destroys an enemy, I have a variable that increases by 1.

If two successive enemy destructions (and variable increases) happen too fast (eg in less than a second), I want something to happen (eg activate a sound, or change a boolean var). I think that I should somehow use deltaTime.

How do I check how much time passed between 2 successive value changes of a variable (in this case chainHits var)?

Thanks for reading :-)

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

Answer by Ray-Pendergraph · Dec 18, 2010 at 01:14 PM

If you are using C# and are using properties, you can just capture a DateTime inside the property setter, or as part of the property changed event. Then every time it's set just see if the last set was before a certain time. If it is, do something.

If you are not using C#, this (data encapsulation) is just one of the great reasons you should consider the switch ;-). Seriously, if you are not and are using UnityScript you will need to encapsulate the access of that data inside of a method (like a setter) and do what I described above. Just exposing a public variable will not do this.

The last and really nasty, not elegant solution would be to note the value in a second variable, then periodically, perhaps in Update or LateUpdate see if the original is different note the time.

if (variable != shadowVariable)
{
   //is the timespan since the last noted change less than my 
   //predefined period.
   if (currentTime - lastChangedTime < predefinedTimePeriod)
   {
      DoThing();   
   }
   shadowVariable = variable;
   lastChangedTime = currentTime;
}

That's psuedocode of course, but hopefully you get the idea.

Comment
Add comment · Show 3 · 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 schwertfisch · Dec 18, 2010 at 02:45 PM 0
Share

thank you Ray, but I don't quite understand what exactly I must do. I think that this is the basic idea that I try to implement. $$anonymous$$aybe it's because I am a bit tired. I'll look into it for the next hours though. Thanks anyway.

avatar image Ray-Pendergraph · Dec 18, 2010 at 04:09 PM 0
Share

$$anonymous$$y guess is that you are using UnityScript? If so the code above should be really close to what you need where 'variable' is the main variable, 'shadowvariabl'e is a variable with the same type and currentTime will actually be var currentTime will be a System.DateTime assigned like this 'currentTime = DateTime.Now; DoThing(); is a method that get's called when successive updates are sufficiently close together... that's pretty much it.

avatar image schwertfisch · Dec 20, 2010 at 09:08 PM 0
Share

Thanks again Ray, as you said, that was pretty much it, only I had to take into account the order in which things happen in my script.

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

No one has followed this question yet.

Related Questions

How do I set a max limit for my health value? 4 Answers

Time of day script not working 2 Answers

Positive to negative not working? 1 Answer

Make a change every 0.02 seconds precise 1 Answer

Static Variable Doesn't Change ? 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