Search Unity

Stop timer when player goes in the "winzone"

Discussion in '2D' started by domy01, Oct 12, 2016.

  1. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Hello i have to do a script:

    I want to stop the timer when the player join in the "winzone" ... and make it appear in another canvas.
    how can i do it? thanks.
    (sorry for my english)

    this is the timer script :

    1. usingUnityEngine;
    2. usingSystem.Collections;
    3. usingUnityEngine.UI;

    4. public class timer :MonoBehaviour{
    5. public Text timerLabel;

    6. private float time;

    7. void Update(){
    8. time +=Time.deltaTime;

    9. var minutes = time /60;//Divide the guiTime by sixty to get the minutes.
    10. var seconds = time %60;//Use the euclidean division for the seconds.
    11. var fraction =(time *100)%100;

    12. //update the label value
    13. timerLabel.text =string.Format("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
    14. }
    15. }
     
  2. rblkwa

    rblkwa

    Joined:
    Jun 20, 2013
    Posts:
    61
    Make sure you have a trigger (a collider with the Is Trigger checkmark checked) in you ' winzone'.
    Add a script to this winzone and get the trigger enter:
    Code (CSharp):
    1.  
    2. // assign the timer-script
    3. [SerializeField]timer myTimer;
    4.  
    5. void OnTriggerEnter(Collider other) {
    6.  if(other.tag == "Player") // or whatever you can use to detect the player
    7.  {
    8.    myTimer.Stop();
    9.  }
    10. }
    11.  
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    Code (CSharp):
    1.  
    2. // modify your timer.cs
    3. // add a bool which indicates if the timer is active.
    4. private bool isTimerActive;
    5.  
    6. // add this new Stop method to disable the timer
    7. public void Stop() {
    8.   isTimerActive = false;
    9. }
    10.  
    11. // modify your update() method, to check the isTimerActive
    12. void Update() {
    13.  
    14.    if(!isTimerActive) // skip out if the timer isn't active
    15.     return;
    16.  
    17.    // ... rest of your code
    18. }
    19.  
    Good luck
     
  3. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    thanks but it doesn't work... The timer doesn't stop
    this is the time script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class timer : MonoBehaviour
    8. {
    9.  
    10.     public Text timerLabel;
    11.  
    12.     private float time;
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         time += Time.deltaTime;
    18.  
    19.         var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
    20.         var seconds = time % 60;//Use the euclidean division for the seconds.
    21.         var fraction = (time * 100) % 100;
    22.  
    23.         //update the label value
    24.         timerLabel.text = string.Format("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
    25.  
    26.         if (!isTimerActive) // skip out if the timer isn't active
    27.             return;
    28.     }
    29.  
    30.     // modify your timer.cs
    31.     // add a bool which indicates if the timer is active.
    32.     private bool isTimerActive;
    33.  
    34.     // add this new Stop method to disable the timer
    35.     public void Stop()
    36.     {
    37.         isTimerActive = false;
    38.     }
    39.  
    40.     // modify your update() method, to check the isTimerActive
    41.  
    42.  
    43. }
    this is the "stop time" script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class StopT : MonoBehaviour
    6. {
    7.    
    8.  
    9.     public void Stop()
    10.     {
    11.         isTimerActive = false;
    12.     }
    13.  
    14.     private bool isTimerActive;
    15.    
    16.     // assign the timer-script
    17.     [SerializeField]
    18.     timer myTimer;
    19.  
    20.     void OnTriggerEnter(Collider other)
    21.     {
    22.         if (other.tag == "Player") // or whatever you can use to detect the player
    23.         {
    24.             myTimer.Stop();
    25.         }
    26.     }
    27. }
    I have made mistakes?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Your update loop is still running all the code before you check the "isTimerActive" boolean. Put that check at the top of the function.
     
  5. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    first don't try to write the timer the way your doing it.

    Code (CSharp):
    1.  
    2. private DateTime mTime;
    3.  
    4. void Awake()
    5. {
    6. mTime = DateTime.Now;
    7. }
    8.  
    9. void Update()
    10. {
    11. if (Date.Now > mTime.addseconds(100))
    12. {
    13.   // do something
    14. // timerLabel.text =mTime.tostring("mm:ss:tt");
    15. // reset timer mTime = Date.Now.addseconds(100);
    16. }
    17. }
    18.  
    I just wrote this into the code editor poster so it might not work out of the box but you get the idea
     
  6. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17

    Thanks i will try...

    Now i have an other problem:
    Can I make a script that allows me to change a button with an other button (when the player goes in the winzone)?
    I want to create an unlock/lock levels system ... Thanks
     
  7. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Yes you can make sure the button is disabled first when scene loads then when your in the winzone enable the button.
     
  8. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Really? How can I do it?
     
  9. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Something like this should work, off my head

    Code (CSharp):
    1.  
    2. private Button mButton;
    3.  
    4. public void Awake()
    5. {
    6.  mButton= GameObject.Find("name of the button").GetComponent<Button>();
    7.  mButton.enabled = false;
    8. }
    9.  
    10. // now somewhere in your code
    11. // mButton.enabled = true;
    12.  
     
  10. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    I write this script .. it is correct?

    Code (CSharp):
    1. public class ButtonChange : MonoBehaviour
    2. {
    3.  
    4.     private Button Level1;
    5.  
    6.     public void Awake()
    7.     {
    8.         Level1 = GameObject.Find("name of the button").GetComponent<Button>();
    9.         Level1.enabled = false;
    10.     }
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if (other.tag == "Player")
    15.         {
    16.             Level1.enabled = true;
    17.         }
    18.     }
    19. }
     
  11. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Yeah looks okay, but don't fogey "name of button" is what you have called it in the inspector, I don't know what it's called so I put that.
     
  12. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Ok thank you so much...
    But i have write this and it doesn't work... (I attacked the script on the Button)

    Code (CSharp):
    1. public class ButtonChange : MonoBehaviour
    2. {
    3.  
    4.     private Button Level2Btn;
    5.  
    6.     public object Winzone;
    7.  
    8.     public void Awake()
    9.     {
    10.         Level2Btn = GameObject.Find("Level2Btn").GetComponent<Button>();
    11.         Level2Btn.enabled = false;
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         if (other.tag == "Player")
    17.         {
    18.             Level2Btn.enabled = true;
    19.         }
    20.  
    21.  
    22.     }
    23. }
    I made mistakes? Thanks for the answers
     
    Last edited: Oct 26, 2016
  13. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    So is it working now?

    Is the name of the button between your quotes "Level2Btn"?

    That might be the issue here.
     
  14. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    The Button is disabled... but when i go in the winzone (and I change scene whit an other button) The button isn't enabled.. Can you help me? Thanks.
     
    Last edited: Oct 26, 2016
  15. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    If you want to send me your project ill try to help you, it might be the easiest way
     
  16. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Okay but My problem is that the winzone is in another scene.... if the winzone wasn't in another scene I can do button disable/enable... How to resolve this problem?
     
  17. domy01

    domy01

    Joined:
    Oct 12, 2016
    Posts:
    17
    Ok now I have a problem : NullReferenceException: Object reference not set to an instance of an object
    It doesn't find the Button.. what can i change in the script?
     
  18. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Your name in the inspector is wrong then, I bet it errors out on line 11?