• 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 G4M3R72 · Mar 06, 2019 at 06:45 AM · timer countdownreverse

Reversing countdown timer with an OnTriggerEnter event so the timer adds time via Time.deltaTime until it reaches max assigned time.

Hi, I have the following timer script that counts down via an image the slides. I would like the timer to count back up toward my publicly assigned maxTime so long as the player is wit$$anonymous$$n a game object's box collider. I've tested the trigger even between the player and the object collider and it is working, but I am having difficulty figuring out the right code to reverse the countdown timer to have it add time. I have two scripts, one for the timer and the other to be attached to the player. The one attached to the player is where I am stuck. Could use some advice.

Timer Script (T$$anonymous$$s works so long as it is counting down. Cannot figure out how to reverse the countdown.)

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class StatusBarScripts : MonoBehaviour {

 public Image spiritualStrengthBar;
 public float maxTime = 60f;
 public GameObject spiritualPool;


 private float timeLeft;


 IEnumerator coroutine;
 // Start is called before the first frame update
 void Start()
 {
     StartCoroutine(DelayTimer());
     spiritualStrengthBar = GetComponent<Image>();
     timeLeft = maxTime;
 }

 // Update is called once per frame
 void Update()
 {
     if (timeLeft > 0)
     {
         timeLeft -= Time.smoothDeltaTime;
         spiritualStrengthBar.fillAmount = timeLeft / maxTime;
     }
     else
     {
         Time.timeScale = 0;
     }

 }
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player" && other.gameObject.tag == "SpiritPool")
     {
         print("Event Triggered");

     }
 }

 IEnumerator DelayTimer()
 {
     yield return new WaitForSeconds(15);
 }


}

Script attached to player (I've tried many different ways to make t$$anonymous$$s work, but none of them have done the trick.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class SpiritPool : MonoBehaviour {

 public Image spiritualStrengthBar;
 public float maxTime = 60f;
 public GameObject spiritPool;

 private float timeLeft;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         print("Event Triggered");
         timeLeft -= Time.smoothDeltaTime;
         spiritualStrengthBar.fillAmount = timeLeft / maxTime;
     }
 }

}

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
0

Answer by bpaynom · Mar 06, 2019 at 08:56 AM

First of all, DelayTimer does not$$anonymous$$ng for you. Check the CoroutinesManual.

On the other hand, you are modifying the fillamount of the image with two "random" values. Instead, I'd suggest you to update the timeLeft of your StatusBarScripts from a reference in the player script.

 // In your StatusBarScripts
 public void IncrementTime ( float amount )
 {
     timeLeft += amount;
 }


And then

 //In your Player Script, in your Trigger function
 myReferenceToStatusBarScripts.IncrementTime( -Time.smoothDeltaTime );


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

100 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

Related Questions

A ball drops down, how to reverse the velocity? 2 Answers

Animation reverse 1 Answer

Reverse physics ? 7 Answers

How to get a countdown timer 2 Answers

player life lose when timer is run out 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