Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

timer

Discussion in 'Getting Started' started by joytdecastro, Feb 28, 2015.

  1. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    i have a power up, and i want to put a timer on it, i have a code there, but the timer is not working
    here's the code for the player....
    Code (JavaScript):
    1. #pragma strict
    2. static var attractCoins: boolean = false;
    3. var timer: float = 15.0;
    4.  
    5. function Start () {
    6.     attractCoins = false;
    7. }
    8.  
    9. function OnTriggerEnter(info : Collider)
    10. {
    11.     if(info.tag == "Magnet")
    12.     {
    13.         attractCoins = true;
    14.     }
    15. }
    16. function Update () {
    17.  
    18.     timer -= Time.deltaTime;
    19.    
    20.     if (timer <= 0)
    21.     {
    22.         timer = 0;
    23.         attractCoins = false;
    24.     }
    25. }
    26.  
    27. function onGUI()
    28. {
    29.  
    30.     GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0"));
    31.  
    32. }
    33.  
    and here's for the coin script...

    Code (JavaScript):
    1. #pragma strict
    2. var target : GameObject;
    3.  
    4. function Start () {
    5.     target = GameObject.FindGameObjectWithTag("Player");
    6.    
    7. }
    8. function Update () {
    9.     if(car.attractCoins == true && Vector3.Distance(transform.position,target.transform.position) < 5.0)
    10.     {
    11.         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * 5);
    12.     }
    13. }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not working is vague. What exactly is happening? What do you want to happen?

    At a glance I see onGUI. This should be OnGUI.
     
  3. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    ow yeah sorry, ok first, the OnGUI is not viewing on the game play, second the attractCoins was still collecting coins.
    Code (JavaScript):
    1. if (timer <= 0)
    2.     {
    3.         timer = 0;
    4.         attractCoins = false;
    5.     }
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sweet.

    Changing onGUI to OnGUI will fix the first problem. Scripting is quite finicky when it comes to capital letters.

    I don't see anything obvious that would mess up the end of the attraction. You may need to sprinkle in Debug.Log quite liberally to help you figure out what is happening.
     
  5. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    ow thank you BoredMormon it works
     
  6. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @
    joytdecastro
    For the education of others, can you please post your amended solution ?

    Thanks
     
  7. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    yeah sure..

    Code (JavaScript):
    1. #pragma strict
    2. static var attractCoins: boolean = false;
    3. var timer = 15.0;
    4.  
    5. function Start () {
    6.     attractCoins = false;
    7. }
    8.  
    9. function OnTriggerEnter(info : Collider)
    10. {
    11.  
    12.     if(info.tag == "Magnet")
    13.     {
    14.         attractCoins = true;
    15.        
    16.     }
    17. }
    18. function Update () {
    19.  
    20.     if (attractCoins)
    21.     {
    22.         timer -= Time.deltaTime;
    23.             if (timer <= 0)
    24.             {
    25.                 timer = 0;
    26.                 attractCoins = false;
    27.             }
    28.     }
    29. }
    30.  
    31. function OnGUI(){
    32.    
    33.  
    34.     if (attractCoins)
    35.     {
    36.     GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0"));
    37.     }
    38. }
    39.  
     
  8. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    timer will probably ALWAYS be less than 0 when you get to the if (timer <= 0) line.

    Even though you set it equal to 0 within the IF statement, the next time it goes thru Update() timer will get set to -Time.deltaTime again. So unless each loop thru Update takes 0 time, it will always be set to this time change.