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 /
  • Help Room /
avatar image
0
Question by HumblPi · Feb 05, 2016 at 04:15 PM · unity 5time.deltatimetime.timescale

Countdown variable modified with deltaTime continuously being reset

Hi all,

I'm having an issue with deltaTime. I'm creating a script that limits a player's cursor speed so that there's a .2 second delay between when the player can move the cursor again, however delta time never seems to reduce my countdown. My outputs of player1MoveDelay and player2MoveDelay when I execute print for each number are constantly somewhere around .82 - .83 for the number itself. What can I do to fix this?

I listed the code below, stripped of everything but the relevant lines.

Thank you.

  using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     
     public class SelectControl : MonoBehaviour {
     
         public float MAX_VALUE = 0.2f;
         public float player1MoveDelay = 0.2f;
         public float player2MoveDelay = 0.2f;
     
         // Use this for initialization
         void Start () {
             Time.timeScale = 1;
         }
     
     
         void checkMotionControls ()
         {
             //Player 1 controls
             if (player1MoveDelay <= 0.0f) {
                     player1MoveDelay = MAX_VALUE; //then proceed
             }
     
             //Player 2 controls
             if (player2MoveDelay <= 0.0f) {
                     player2MoveDelay = MAX_VALUE; //then proceed
 
             }
         }
     
         // Update is called once per frame
         void Update () {
             checkMotionControls ();
             checkTime (player1MoveDelay);
             checkTime (player2MoveDelay);
         }
     
         void checkTime (float num)
         {
             if (num > 0) {
                 num -= (Time.deltaTime);
                             //for debugging purposes
                 print ("Time " + Time.deltaTime);
                 print ("Number " + num);
             }
         }
     }
 



Here is an example of the output I will get:

alt text

deltatime-output.png (32.8 kB)
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

Answer by Jessespike · Feb 05, 2016 at 06:20 PM

player1MoveDelay and player2MoveDelay never actually change. You're passing them into checkTime() which copies the value and stores it in the local variable "num".

You can pass by reference instead, I think that's what you were trying to do:

              ...
              checkTime (ref player1MoveDelay);
              checkTime (ref player2MoveDelay);
          }
      
          void checkTime (ref float num)
          {
              ...
Comment
Add comment · Show 2 · 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 NoseKills · Feb 05, 2016 at 06:42 PM 1
Share

@HumblPi To specify a bit, this happens with all 'value types', such as Vectors, ints, Colors etc.

With a reference type you would actually be able to modify the object inside the function.

Reference vs. Value types

avatar image HumblPi · Feb 07, 2016 at 12:01 AM 0
Share

Thank you so much! I've been working on this thing for so long, tunnel vision set in and I missed something pretty obvious like that.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# I need to move object in a cycle 0 Answers

Slow Motion Issues! 1 Answer

Time.deltatime not working. 1 Answer

How to Make Timer (Score) Count Up Faster? 1 Answer

Increase over time, 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