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 /
This question was closed May 09, 2018 at 07:24 PM by tormentoarmagedoom for the following reason:

The question is solved

avatar image
0
Question by mjonasz · May 07, 2018 at 04:59 PM · timer countdown

Timer in UI panel does not reset

I have a script where, when an the player takes control of the final objective, the finalObjectiveTimer is supposed to start counting down. And it works. The panel appears and the timer counts down. When the player loses the objective and then retakes it again, the timer is supposed to restart. And it does - but only in the inspector. In the panel it continues where it left off. Any ideas why the one in the UI panel refuses to restart as well?

  private void Update()
     {
         {
             if (finalObjectiveTimerActivated == true)
             {
                 timer = startTimer;
                 timer -= Time.deltaTime;
                 finalObjectiveTimer.text = timer.ToString("f0");                   
             }
             if (finalObjectiveTimerActivated == false)
             {
                 timer = startTimer;
                 finalObjectiveTimer.text = timer.ToString("f0");
             }
         }
     }

 


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

  • Sort: 
avatar image
0
Best Answer

Answer by Eno-Khaon · May 07, 2018 at 06:07 PM

It feels like this shouldn't be the script where you're seeing this problem. In fact, I can't see how this script would actually "behave" in the inspector.

If the timer is activated, you set the time to the starting value, then decrease it by a fraction of a second. If, say, the timer is 10 seconds, that means the value should realistically never drop below about 9.98 (est. 50fps), since it would be reset to 10 every frame.

Are there other scripts you're trying to accomplish a similar goal in? I can't see this excerpt relating to the problem you're describing, unless there's further context that's missing.

Comment
Add comment · Show 5 · 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 mjonasz · May 09, 2018 at 07:17 PM 0
Share

Thanks for your answer - I did not expect something this fast :) You are right, it kept reseting but I didn't notice because I paid attention to the countdown on my UI panel. I corrected this script so it doesn't reset but now my UI timer stopped counting down. It appears with the coundtown value but doesn't count down while the timer in the inspector keeps counting down.

 void Start()
 {
     objectiveTimer = maxObjectiveTimer;
     player2ControlPanel.SetActive(true);
     player1ControlPanel.SetActive(false);     
 }
 private void Update()
 {
     if (changeOfControl == true)
     {
         objectiveTimer -= Time.deltaTime;
     }            
         if (finalObjectiveTimerActivated == true)
         {
         finalObjectiveTimer = GetComponent<Text>();
         timer -= Time.deltaTime;
         finalObjectiveTimer.text = timer.ToString("f0");
        
         if (timer <= 0)
             {
             Time.timeScale = 0;
             missionSuccessPanel.SetActive(true);
         }
             }
             if (finalObjectiveTimerActivated == false)
             {
                 timer = startTimer;
                 finalObjectiveTimer.text = timer.ToString("f0");
             }
     }
     void OnTriggerStay(Collider col)
     {    
         {
             if (col.gameObject.tag == "Player1")            
             if (objectiveTimer < 0)
                 {
                     {
                         player1Control = true;
                         player2Control = false;
                     changeOfControl = false;
                     objectiveTimer = maxObjectiveTimer;
                     if (finalObjective == true)
                         {
                             player1ControlPanel.SetActive(true);
                             player2ControlPanel.SetActive(false);
                             finalObjectiveTimerPanel.SetActive(true);
                             finalObjectiveTimerActivated = true;
                             timer = startTimer;
                         }
                     }
                 }`
 private void OnTriggerEnter(Collider col)
     {
        if (col.gameObject.tag == "Player1" || col.gameObject.tag == "Player2")        
      changeOfControl = true;
     }
 

`

avatar image Eno-Khaon mjonasz · May 10, 2018 at 07:16 PM 0
Share

I'd be rather surprised if you aren't getting Null Reference Exceptions in this case. From what I can tell, the problem primarily lies in this line:

 finalObjectiveTimer = GetComponent<Text>();


You appear to have a variable set aside for finalObjectiveTimerPanel as a GameObject (based on its usage), so you should presumably be calling GetComponent on it, rather than on the currently object, which I assume is a separate Game Controller and not the Timer object itself.

That said, based on what you've provided, it may make more sense to make that GetComponent<>() call when you switch over to the "final objective" portion (currently in OnTriggerStay()), so you're not calling that function every frame for the same object.

avatar image mjonasz Eno-Khaon · May 14, 2018 at 11:57 PM 0
Share

Problem solved. You were right about the finalObjectiveTimer = GetComponent(); line being the problem. As soon as I commented it out the timer started to work perfectly.

avatar image mjonasz mjonasz · May 14, 2018 at 06:52 PM 0
Share

Hi, thanks for your answer.

I moved the GetComponent<>() call as per your suggestion but I'm having a hard time understanding what you mean in that first paragraph - I'm at a very basic level in this stuff. Are you saying that the script that runs the timer should be attached to that timer panel ins$$anonymous$$d to the "FinalObjective" itself?

avatar image Eno-Khaon mjonasz · May 15, 2018 at 10:14 AM 0
Share

There's no need for that if you have a reference to that object. As long as the GameObject is assigned properly, you can get a component from another one easily enough.

 finalObjectiveTimer = finalObjectiveTimerPanel.GetComponent<Text>();

Follow this Question

Answers Answers and Comments

81 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

Related Questions

Text UI shakes when being updated with a countdown function 1 Answer

How to get left time from countdown to point? 0 Answers

Need help with a timer in a android 2d game 0 Answers

Timer is not counting down. Staying at original number. 1 Answer

How to create a 30 min demo scene? 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