Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 patrapol · Oct 01, 2013 at 03:50 AM · timepickupadd

Adding time to timer with pickup

Hello, I'm trying to used the script attach to my player to +10 to my timeLeft on the other script attach to the GUI. The problem is it compile ok but the timer does not increase after the pickup. Any guidance will be greatly appreciate. Thank you

Player

using UnityEngine; using System.Collections;

 public class Player : MonoBehaviour 
 
 {
 
 CountdownTimer_test countdownTimer_test;
 
 void Update () 
         {
         // call countdownTimer_test
         countdownTimer_test = GetComponent<CountdownTimer_test>();
         }
 
 //pick up
     void OnTriggerEnter(Collider other)
         
     {
         if(other.gameObject.tag == "PickUp")
         {
         //add time
             
             Destroy(other.gameObject);
             //
             countdownTimer_test.timeLeft += 10;    
         }    
     }
 }


CountdownTimer_test(GUI)

 using UnityEngine;
 using System.Collections;
 
 public class CountdownTimer_test : MonoBehaviour 
 {
     public float timeLeft = 50.0f;
 
  
 
     public void Update()
 
     {
 
         timeLeft -= Time.deltaTime;
 
  
 
         if (timeLeft <= 0.0f)
 
         {
 
             // End the level here.
 
             //guiText.text = "You ran out of time";
             Application.LoadLevel("learningunity_02");
 
         }
 
         else
 
         {
 
             guiText.text = "Time left = " + (int)timeLeft + " seconds";
         }
     }
 } 
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 Fattie · Oct 01, 2013 at 09:23 AM 0
Share

Never use yield or coroutines. They are only for specific advanced use.

You use

Invoke()

.

For timers in Unity. It's incredibly simple. Search on this site for 10000s of examples.

http://answers.unity3d.com/questions/543410/need-delay-for-powerup.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TrickyHandz · Oct 01, 2013 at 04:24 AM

The reference to "CountdownTimer_Test" is not going to have a reference on the player object when you are trying to assign it because GetComponent() is only looking for components on the object that has the "Player" script attached to it. You can solve this by making a public variable to hold a reference that you can assign in the inspector like this:

 using UnityEngine; 
 using System.Collections;
 
 public class Player : MonoBehaviour 
 {
  
 public CountdownTimer_test countdownTimer_test;
  
 void Update () 
 {
    // You won't need the assignment in
    // Update() anymore since it can be
    // assigned in the Inspector window
 }
  
 //pick up
 void OnTriggerEnter(Collider other)
 {
    if(other.gameObject.tag == "PickUp")
    {
    //add time
      Destroy(other.gameObject);
      countdownTimer_test.timeLeft += 10;  
    }
 }

By adding the variable above, you can simply drag the object that has the "CountdownTimer_Test" component on it into the variable slot in the inspector and Unity will find the reference to the script.

Comment
Add comment · Show 2 · 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 patrapol · Oct 01, 2013 at 05:38 AM 0
Share

Hello, TrickyHandz

Thank you very much for the help and explanation I am very new at this. Your instruction is very easy to follow,it work now.

Thank again,have a good day!

avatar image TrickyHandz · Oct 01, 2013 at 12:38 PM 0
Share

Glad I could help. Happy coding!

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

16 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

Related Questions

How can I let users add numbers, but in time/hours/minutes?,When button pressed, add X amount of time. 1 Answer

Add to score every second 3 Answers

Adding a value to a variable every second? 1 Answer

How to add time to a timer by picking up coins 1 Answer

Calculate in Seconds not Frames? 2 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