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 rOBY GAMES · Oct 13, 2013 at 10:33 PM · triggercountdownactivateseconds

script countdown

Hello!

I need some help. In this script countdown, in my game is activated by triggers and also always disabled with trigger. everything works fine, but when reactivated through triggers the sript countdown will not start again from 60 seconds, but always remains zero. How do I update this script so that it always start again from 60 seconds every time the script reactivated? thank you!

My current script.

 public var currentTime : int = 60;
 
 var style : GUIStyle;    
 
 function Start()
 {
    InvokeRepeating( "CountDown", 1, 1 );
 }
 
 function CountDown()
 {
    if (--currentTime == 0)
    {
        Debug.Log("Time is up my friend");
        CancelInvoke( "CountDown" );
 
    }
 
 }
 
 
 function OnGUI ()
 {
      GUILayout.Label(currentTime.ToString(),style); 
 }
Comment
Add comment · Show 3
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 Cherno · Oct 13, 2013 at 11:09 PM 0
Share

I don't understand what you are trying to do. Do you want the time to count down from 60 when a collider touches the gameobject's collider, which is set to IsTrigger?

avatar image meat5000 ♦ · Oct 14, 2013 at 12:28 AM 0
Share

Have you tried placing currentTime = 60; in the Start()?

avatar image rOBY GAMES · Oct 14, 2013 at 04:47 PM 0
Share

Hello! In short I would like the countdown when turning off the script itself through this other script below, you must return to 60 rather than to zero on the variable, so as to divide by 60 seconds when the script is reactivated.

All this must operate during active play. Thank you.

Script that toggles through triggers the script (Tempos).

Script Name (enable disable script).

 var Player : GameObject;
 var Script : Tempos;
  
 function OnTriggerEnter(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
 {
    if(other.collider.tag == Player.tag) //Checks if the Player is inside the trigger
    {
       Script = GetComponent(Tempos); 
       Script.enabled = true; //enables theScript.
    
    
    }
 }
 
 function OnTriggerExit(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
 {
    if(other.collider.tag == Player.tag) //Checks if the Player is inside the trigger
    {
       Script = GetComponent(Tempos);
       Script.enabled = false; //disable theScript.
    
    
    
    }
 }
 

Script Name countdown (Tempos).

 public var currentTime : int = 60;
 
 
 
  var style : GUIStyle;    
 
 function Start(){
 
         InvokeRepeating( "CountDown", 1, 1 );
 
     }
 
 
 
 function CountDown()
 
     {
 
         if (--currentTime == 0)
 
         {
 
             Debug.Log("Time is up my friend");
 
             CancelInvoke( "CountDown" );
 
         }
 
     }
 
 
  function OnGUI () {
  
   
  GUILayout.Label(currentTime.ToString(),style);
  
  
  }                                                      

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pecek · Oct 14, 2013 at 12:37 AM

You have to set the timer manually every time and it will work. On trigger enter - call the countdown script, and set the value.

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
avatar image
0

Answer by meat5000 · Oct 14, 2013 at 04:50 PM

For performing an operation on script disable, try

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDisable.html

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 rOBY GAMES · Oct 14, 2013 at 05:07 PM 0
Share

I do not understand where this code must be entered. I have to create a new script? I need to add it in one of the two?

OnDisable function () { print ("script was removed"); }

avatar image meat5000 ♦ · Oct 14, 2013 at 05:09 PM 0
Share

Place it at the end of your script. It will be called when the script is disabled.

It's similar to OnDestroy()

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

18 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

Related Questions

Activate script when player enters trigger zone. 2 Answers

How to stop a Countdown Timer? 1 Answer

activate script from trigger 1 Answer

Enable/Disable script with timer 1 Answer

Play parent animation without affecting the child animation 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