Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 fwooshchannel · Dec 09, 2021 at 05:33 PM · timer

How can i restart a timer?

So I'm trying to make a timed pressure pad that can either go for 3 seconds or 5 seconds, depending on the configuration, and this all works, but it doesn't reactivate if I step on it after it ended the first time, and I can't figure out where I went wrong, if you could help, that's be great!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class TimedPressurePad : MonoBehaviour
 {
     public float PadTime = 5f;
     public Material pressed;
     public Material unpressed;
     public GameObject Object;
 
     public Material fivePad;
     public Material fourPad;
     public Material threePad;
     public Material twoPad;
     public Material onePad;
 
     private void OnTriggerEnter(Collider other)
     {
         if (PadTime == 5f)
             Object.GetComponent<Renderer>().material = fivePad;
         else if (PadTime == 3f)
             Object.GetComponent<Renderer>().material = threePad;
         StartCoroutine(PadTimer());
     }
 
     IEnumerator PadTimer()
     {
 
 
         while (PadTime !=0f)
         {
             yield return new WaitForSeconds(PadTime / PadTime);
             PadTime = PadTime - 1f;
             Debug.Log(PadTime);
 
             if (PadTime == 5f)
                 Object.GetComponent<Renderer>().material = fivePad;
             else if (PadTime == 4f)
                 Object.GetComponent<Renderer>().material = fourPad;
             else if (PadTime == 3f)
                 Object.GetComponent<Renderer>().material = threePad;
             else if (PadTime == 2f)
                 Object.GetComponent<Renderer>().material = twoPad;
             else if (PadTime == 1f)
                 Object.GetComponent<Renderer>().material = onePad;
                 
         }
 
         Object.GetComponent<Renderer>().material = unpressed;
         StopCoroutine(PadTimer());
     }
 }
 


Comment
Add comment · Show 2
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 CodesCove · Dec 09, 2021 at 07:49 PM 0
Share

You don't re-initialize PadTime to 5

avatar image jmhoubre · Dec 09, 2021 at 08:21 PM 0
Share

Try to rename Object in something else :

public GameObject myObject;

Object is a key word (it is not colorized like the other fields).

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Dec 09, 2021 at 08:41 PM

A few things are to be noted about your code:


First up is that you should not have a while loop with the condition != 0f This is not safe imo when dealing with floats. Please go for >= 0f instead.


Secondly why WaitForSeconds(PadTime / PadTime); This is WaitForSeconds(1f); right?


But most importantly:

  StopCoroutine(PadTimer());

This does not do what you think it does. If you want this coroutine to end then just exit the function. This can be forced with yield break; as a similarity to return in a normal function but just letting the function end like any other as well is enough.

If you want to use StopCoroutine correctly you have to do it like this:

  routineReference = PadTimer()
  StartCoroutine(routineReference);
  ...
  // do something and somewhere else cancel it:
  ...
  StopCoroutine(routineReference);


Most importantly your issue is Probably that you do not reset PadTimer once it has reached zero. This is probably the issue why you cannot restart it since it always directly exits your Routine.

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 Hellium · Dec 09, 2021 at 08:57 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TimedPressurePad : MonoBehaviour
 {
     public float padTime = 5f;
     public Material pressed;
     public Material unpressed;
     public Renderer padRenderer;
 
     // Fill the inspector with the one, two, three, four and five pads
     // First element = [0..1[ second
     // Second element = [1..2[ second
     // ...
     // Fifth element = [4..5[ second
     // ... 
     public Material[] timerMaterials;
 
     private void OnTriggerEnter(Collider other)
     {
         StopAllCoroutines();
         StartCoroutine(ChangePadMaterial());
     }
 
     IEnumerator ChangePadMaterial()
     {
         int index = Mathf.Min(Mathf.FloorToInt(padTime - 0.01f), timerMaterials.Length - 1);
 
         if(index >= 0 && !Mathf.Approximately(index, padTime))
         {
             padRenderer.material = timerMaterials[index];
             yield return new WaitForSeconds(padTime - index);
             index--;
         }
 
         WaitForSeconds wait = new WaitForSeconds(1);
         for(; index >= 0 ; index -= 1)
         {
             padRenderer.material = timerMaterials[index];
             yield return wait;
         }
         
         padRenderer.material = unpressed;
     }
 }
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

134 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 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

GUI elements vanish when publishing 1 Answer

Timer Between Labels 2 Answers

Need some help an object collider that stops a timer then displays it on another scene 1 Answer

countdown/countup timer 1 Answer

Timer Pause 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