• 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 Forja Entertainment · Apr 11, 2014 at 09:36 AM · timergui text

How do I add time to timer with C#?

I'm trying to add time to a timer, which I named and tagged Timer. This script will be attached to various game objects. When the player collides with these objects it should add 10/20 seconds on the GUI timer. I've been trying to figure this out for 3 days, I found examples in Java which I've tested with no luck. I've watched tutorials, tried a lot of things out, it always shows errors. This is the first time i've done programming so learning a lot, this is the last bit of code left for the gameplay. I would appreciate some help or advise in making this work.

What I got here works so far to destroy objects on collision and add points, just need it to add time.

Harry

 using UnityEngine;
 using System.Collections;
 
 public class ScorePoint : MonoBehaviour {
     
     void OnTriggerEnter2D(Collider2D collider) {
                 if (collider.tag == "Player") {
                         Score.AddPoint ();
                         gameObject.SetActive (false);
                 }
 
         }
 }




This is my timer script:

using UnityEngine; using System.Collections;

public class Timer : MonoBehaviour { public float Seconds = 59; public float Minutes = 1;

 void Update()
 {
     if(Seconds <= 0)
     {
         Seconds = 59;
         if(Minutes >= 1)
         {
             Minutes--;
         }
         else
         {
             Minutes = 0;
             Seconds = 0;
             GameObject.Find("Timer").guiText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");
         }
     }
     else
     {
         Seconds -= Time.deltaTime;
     }

     

     if(Mathf.Round(Seconds) <= 9)
     {
         GameObject.Find("Timer").guiText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");
     }
     else
     {
         GameObject.Find("Timer").guiText.text = Minutes.ToString("f0") + ":" + Seconds.ToString("f0");
     }
     {
         if(Seconds <= 0.0) {
             Application.LoadLevel("GameOver");
         }
 }

} }

Comment
Add comment · Show 1
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 Simon-Larsen · Apr 11, 2014 at 11:05 AM 0
Share

Why not just count the sum of seconds passed and then devide it into whatever you want when you display the gui?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by maddFrogg · Apr 11, 2014 at 12:00 PM

You should not attach the timer to various GameObjects, this way you will have various timers. Attach the Timer script to an empty object and collision detectors to those various objects. When detecting collision, refer to the Timer class from the collision detector class and do the stuff.

Comment
Add comment · Show 1 · 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 robhuhn · Apr 11, 2014 at 12:05 PM

Didn't test this snippet but it might be what you're looking for.

 using System;

 private float seconds = 59f;

 void Update()
 {
     if(seconds > 0f) 
     {
         seconds -= Time.deltaTime;

         TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
         string timeLeft = string.Format("{0:D2}:{1:D2}", timeSpan.Minutes, timeSpan.Seconds);
         GameObject.Find("Timer").guiText.text = timeLeft;
     }
     else
     {
         Application.LoadLevel("GameOver");
     }
 }


 public void AddBonusTime(float bonusTime)
 {
     seconds += bonusTime;
 }

As @maddFrogg suggested, there should be only one timer object.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Countdown Timer location 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

My timer freezes everytime I leave the scene and come back. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges