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
0
Question by BGR521 · Nov 30, 2018 at 03:56 AM · triggertimercoin

Can't make time go down after picking up a coin,

So what i have is a "coin" which is just a game object to act as a trigger with a script attached to it that does 3 things: play a sound, call a function from my timer script, and then destroy the coin. The audio is played, the coin disappears, but my timer just keeps going with the same time. I think its worth noting that I'm almost brand new to Unity and coding in general, so I still don't understand a large chunk of c#.

Here is my timer

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
     public Text counterText;
     public float seconds, minutes;
     bool keepTiming;
     private float _time = 0.0f;
 
     // Use this for initialization
     void Start ()
     {
         counterText = GetComponent<Text>() as Text;
         StartTimer();
     }
 
 
     void StartTimer()
     {
         keepTiming = true;
     }
    
     private void Update()
     {
         if (keepTiming)
         {
             UpdateTime();
         }
     }
 
     // Update is called once per frame
     void UpdateTime()
     {
         _time += Time.deltaTime;
         minutes = (int)(Time.time / 60f);
         seconds = (int)(Time.time % 60f);
         counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
     }
 
     public void SubtractTime()
     {     
        _time -= 5.0f;
     }
 
     public void StopTimer()
     {
         keepTiming = false;
     }
 }


And here's the script i placed on my coin

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CoinCollect : MonoBehaviour {
 
     public Timer script;
     public GameObject trigger;
     public AudioSource audioClip;
 
     private void OnTriggerEnter(Collider collider)
     {
         if (collider.tag == "Player")
         {
             trigger.SetActive(true);
             audioClip.Play();
             script.SubtractTime();
             Destroy(gameObject);
         }
     }
 
     private void OnTriggerExit(Collider collider)
     {
         if (collider.tag == "Player")
         {
             trigger.SetActive(false);
         }
     }
 }

I'm just really lost on how to fix this, so any advice is greatly appreciated.

,

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Nov 30, 2018 at 05:08 AM

Your minutes and seconds variables are using Time.time rather than your custom-defined _Time variable.

Comment
Add comment · Show 3 · 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 BGR521 · Nov 30, 2018 at 12:30 PM 0
Share

Oh, yah that would make sense. I'll try try to fix that whenever I have a chance. Would i just replace time in Time.time with _time to make it work? Thanks again for the help.

avatar image Eno-Khaon BGR521 · Nov 30, 2018 at 07:26 PM 0
Share

Yep.

Fundamentally, your _Time variable should be the same value as Time.time when you add Time.deltaTime every frame, but then any further changes made to it (removing time for grabbing coins) would be specific to your own, so it's what you should be showing the player.

avatar image BGR521 Eno-Khaon · Nov 30, 2018 at 08:36 PM 0
Share

Ok, ill try that. Thanks!

avatar image
0

Answer by BGR521 · Nov 30, 2018 at 09:01 PM

Update: I managed to get it to work now. I was getting a NullReference error but i guess i just didn't have the script selected on my coin. Thank you again for the help.

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

117 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

Related Questions

How to add 5 seconds to Timer that is counting DOWN OnTriggerEnter2d? 1 Answer

How to handle animation of multiple Coins? 1 Answer

How to repeat damage 1 Answer

Enable/Disable script with timer 1 Answer

close game in delay after pressing button 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