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 harrietradcliffe · Sep 03, 2013 at 04:14 PM · timersubtractingsubtract

Timer not subtracting...

Hi! we have a code for timer. it's a timer for our hint. When we click hint... it has to count 3 to 1. This is our code.

 private var count: float;
 
 private var time: float;
 
 var c: TextMesh;
 
 
 
 function Start(){
 
 time=0;
 
 count=3;
 
 }
 
 
 
 function Update () {
 
     if(Input.GetMouseButtonDown(0))
 
     {
 
         var hit: RaycastHit;
 
          var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);
 
          
 
          var background: GameObject;
 
         background = GameObject.Find("Background");
 
         var background2: GameObject;
 
         background2 = GameObject.Find("Background2");
 
        if(Physics.Raycast(ray, hit))
 
        {
 
                if(hit.transform.name == "hint")
 
              {
 
                  time += Time.deltaTime * 1;
 
                 var counter = Mathf.Round(count - time);
 
                 c.text = counter.ToString();
 
                 
 
                  background.renderer.enabled= false;
 
                  background2.renderer.enabled= true;
 
                  if(counter == 0){
 
                      background.renderer.enabled= true;
 
                      background2.renderer.enabled= false;
 
                  }
 
              }
 
        }
 
     }
 
 }

please help us. The time's not subtracting. Thank you!!

Comment
Add comment · Show 3
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 fafase · Sep 03, 2013 at 04:16 PM 0
Share

$$anonymous$$ake it simple:

 count -= Time.deltaTime;
 int counter = (int)count;
 c.text = counter.ToString();
avatar image harrietradcliffe · Sep 03, 2013 at 04:53 PM 0
Share

hello, i've tried this code/// it only subtracts one time... i want it to be like timer...

avatar image fafase · Sep 03, 2013 at 04:57 PM 0
Share

Well, you need to create a function that gets started when you press the button

 var runnung:boolean = false;
 function Timer()
 {
     while(count > 0){
       count -= Time.deltaTime;
       int counter = (int)count;
       c.text = counter.ToString();
       yield;
    }
    running = false;
 }
 
 
 if(Physics.Raycast(ray, hit)){
    if(hit.transform.name == "hint"){
      if(!running){
         Timer();
         running =true;
      }
    }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by -hiTo- · Sep 03, 2013 at 04:56 PM

First off, you are never subtracting anything. You are adding.

time += Time.deltaTime 1; (Why 1? Something * 1 is itself...?);

Second; you are only doing it while you are clicking something (you are only doing it while Input.GetMouseButtonDown(0) is true).

My suggestion is to replace the time += Time.deltaTime * 1. And you should end up with this:

     private var count: float;     
     private var remainingTime : float;
     private var background: GameObject;
     private var background2: GameObject;
     var c: TextMesh;
      
     function Start()
     {
         background = GameObject.Find("Background");
         background2 = GameObject.Find("Background2");
         remainingTime =0;     
         count=3;     
     }
 
     function Update () 
     {
         if (remainingTime >= 0)
         {
             remainingTime -= Time.deltaTime;
             var counter = Mathf.Round(remainingTime);     
             c.text = counter.ToString();
         }
         else
         {
             //The Timer Has Finished.
             background.renderer.enabled= true;
             background2.renderer.enabled= false;
         }
      
         if(Input.GetMouseButtonDown(0))     
         {     
             var hit: RaycastHit;     
             var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);
      
             if(Physics.Raycast(ray, hit))     
             {     
                 if(hit.transform.name == "hint")     
                 {     
                     remainingTime = count;  // Replace time += Time.deltaTime with this
 
                     background.renderer.enabled= false;
                     background2.renderer.enabled= true;
                 }
             }
         }
     }
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 harrietradcliffe · Sep 03, 2013 at 05:22 PM 0
Share

hello... where is the time in (count-time) co$$anonymous$$g from?

avatar image -hiTo- · Sep 03, 2013 at 05:49 PM 0
Share

I missed that. I renamed the variable to make more sense.

The answer is now updated.

avatar image harrietradcliffe · Sep 04, 2013 at 02:50 PM 0
Share

okaay :)) ill try this code

avatar image -hiTo- · Sep 04, 2013 at 03:08 PM 0
Share

I reordered the code (I put the backgrounds in as private member variables off the class, and initiated them in Start), so that your needs are met better.

avatar image harrietradcliffe · Sep 09, 2013 at 03:47 PM 0
Share

hi! thank you! that code helped us :))

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

17 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

Related Questions

Timer Countdown not subtracting value. 1 Answer

Double subtraction issue 1 Answer

subtracting numbers gives a very wrong answer 0 Answers

Can anyone help me figure this script out? (Light Trigger) 1 Answer

Timer not work :( 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