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
-3
Question by RenatoB · May 26, 2011 at 07:24 PM · timedeltatime

Something wrong with TimedeltaTime

I changed the command several times but nothing happens.

   function Update(){
         if (bar == false){
         currlife = 100;
     }
     
     
 else
     currlife -= 1.0 * Time.deltaTime;
 }

Tried:
currlife -= Time.deltaTime * 0.1;

currlife -= Time.deltaTime;

Comment
Add comment · Show 4
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 DaveA · May 26, 2011 at 07:29 PM 1
Share

What do you believe is wrong? BTW No need to multiply by 1.0.

avatar image RenatoB · May 26, 2011 at 07:38 PM 0
Share

the velocity is the same in all my tries

avatar image flaviusxvii · May 26, 2011 at 07:40 PM 0
Share

velocity? what velocity you talking about?

avatar image RenatoB · May 26, 2011 at 07:43 PM 0
Share

bar decrement

4 Replies

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

Answer by aldonaletto · May 26, 2011 at 10:32 PM

RenatoB, I tested your script and it really runs fast like a crazy horse! The problem is the type of the currlife var: if you don't specify the type, javascript will do it for you, right or wrong! That's a golden rule in unity's javascript: always define the type of any var. When javascript finds an untyped variable, it must decide at runtime which's the type of its contents; it wastes CPU time and may generate errors. I fixed your script and there it goes:

var bar: boolean; var maxlife: float = 100; var currlife: float = 100;

function Start(){ bar = false; }

function Update(){ if (bar == false){ currlife = 100; } else { currlife -= 1.0 * Time.deltaTime; } }

function OnTriggerEnter(){ bar = true; }

function OnTriggerExit(){ bar = false; }

Comment
Add comment · Show 5 · 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 Eric5h5 · May 26, 2011 at 10:51 PM 0
Share

That's not correct...if you don't define the type of a variable, the compiler figures it out from the value you supply. "`var foo = 5;`" is exactly the same as "`var foo : int = 5;`". Likewise, "`var currlife = 100.0`" is the same as "`var currlife : float = 100`". This happens when the script is compiled, not at runtime. It's called type inference, and C# does this too.

avatar image Owen-Reynolds · May 26, 2011 at 11:08 PM 0
Share

C# doesn't guess the types, which is part of the confusion when people go back and forth. In C#, in float T = 0;, the 0 is a shortcut for 0.0 and T is still a float. C#-users get in the habit of not putting ".0" on the end of floats. For fun, int T = 4.0; is an error -- 4.0 isn't an int.

avatar image Eric5h5 · May 26, 2011 at 11:24 PM 0
Share

@Owen Reynolds: no, "`var t = 0.0;`" is valid C#. It works the same way. (Although in this case t would be a double, rather than a single like it would be in JS. Using 0.0f would make it a single.) The difference is that you can only do that with local variables in C#, not global.

avatar image aldonaletto · May 27, 2011 at 01:48 AM 0
Share

I tested it and you both are right: Unity's javascript actually uses type inference. That's different from the interpreted javascript found in browsers, where all variables are variant types which can hold any type known.

avatar image Eric5h5 · May 27, 2011 at 01:56 AM 0
Share

Yeah, Javascript in Unity has almost nothing to do with Javascript in web browsers. It's way more like JScript.NET, or ActionScript 3.

avatar image
2

Answer by DaveA · May 26, 2011 at 07:30 PM

You might want to be sure that currlife is a float when you declare it:

 curlife : float = 100.0;
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
0

Answer by flaviusxvii · May 26, 2011 at 07:29 PM

There is nothing wrong with Time.deltaTime. Check your stuff again.

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
0

Answer by homeros · May 26, 2011 at 07:34 PM

Did you try using it outside of else? The problem amybe bar never becomes true.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to have a gameobject in it's own timeframe? 0 Answers

why is every 2. frame is weird with Time.deltaTime 1 Answer

Movement speed difference between the different devices 1 Answer

rhythm game prefab spawn slower after build 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