• 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 LXwolf1 · Mar 13, 2015 at 05:58 PM · collisiontimetimer

How can I add time to a timer on an object collision?

In my game I have a timer that will countdown from 10, but if the player hits these orbs then I want to have an additional 5 seconds be added to the timer.

Here is my timer code.

using UnityEngine; using System.Collections;

public class Timer : MonoBehaviour {

 public float time = 11;

  IEnumerator Do() {
     yield return new WaitForSeconds(3);
     Application.LoadLevel(Application.loadedLevel);
 }

 // Use this for initialization
 void Start () {
 
 }
 

 void Update () {
 
     time -= Time.deltaTime;
 }

 void OnGUI(){
     if (time > 1) {
         GUI.Label (new Rect (100, 100, 200, 100), "" + (int)time);
     } 

     else {
         GUI.Label (new Rect (100, 100, 200, 100), "Game Over");
         StartCoroutine(Do());
     }
 }

}

Here is my collsion.

using UnityEngine; using System.Collections;

public class Des : MonoBehaviour {

 void OnTriggerEnter(Collider other) {
     Destroy(other.gameObject);

 }

}

Also I was looking for a way to make the timer not be able to go past 20 seconds. For that i was thinking about something like

if(time >= 20){ time = 20; }

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 Qasem2014 · Mar 13, 2015 at 06:26 PM

so add extra time in OnTriggerEnter function ! where is the problem ?

 void OnTriggerEnter(Collider other)
 {
    time+=5; if(time >= 20){ time = 20; } Destroy(other.gameObject,0.1);
 }

and your if is a good idea

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 Pendantic · Mar 13, 2015 at 08:22 PM 0
Share

for implementing a max value you can also use $$anonymous$$athf.$$anonymous$$in as such

 timer = $$anonymous$$athf.$$anonymous$$in(timer+5,20);

This will pick the lowest number of the 2, so it will always be equal or less than 20

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The timer starts after a collision with an object 1 Answer

Time! from Rotation of Light 1 Answer

Start timer with trigger 1 Answer

Time release Collectable 1 Answer

When i run Unity3D leaves a freak message 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