Search Unity

Trouble implementing a time limit

Discussion in 'Scripting' started by Blackthorn_01, Jan 28, 2015.

  1. Blackthorn_01

    Blackthorn_01

    Joined:
    Jan 28, 2015
    Posts:
    4
    Hello Unity Community^^

    Yesterday i checked out the unity engine and was really surprised of how awesome it was. It is relativly easy to use and i never thought i would ever get a 3d Game to work... most of the times. I finished the first Video tutorial and created the Roll-a-Ball game and thought why not play around with the game a bit before moving on. So i made the game bigger, added obstacles... and i wanted to add a Time limit so you have to complete the Game fast (under 40 Seconds) and there's the problem... I cannot get it to work and the behaves very strangely now.

    Here's the source code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Playercontroller : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private int count;
    9.     public Text countText;
    10.     public Text winText;
    11.     public Text timeText;
    12.     private float timelimit;
    13.  
    14.     void Start ()
    15.     {
    16.         count = 0;
    17.         timelimit = Time.fixedTime;
    18.         SetTimeText ();
    19.         SetCountText ();
    20.         winText.text = "";
    21.     }
    22.  
    23.     void FixedUpdate ()
    24.     {
    25.         float moveHorizontal = Input.GetAxis("Horizontal");
    26.         float moveVertical = Input.GetAxis("Vertical");
    27.  
    28.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    29.  
    30.         rigidbody.AddForce (movement * speed * Time.deltaTime);
    31.     }
    32.  
    33.     void OnTriggerEnter(Collider other)
    34.     {
    35.         if (other.gameObject.tag == "PickUp")
    36.         {
    37.             other.gameObject.SetActive(false);
    38.             count = count + 1;
    39.             SetCountText();
    40.         }
    41.     }
    42.  
    43.     //I CANT F***ING TAKE IT ANYMORE!!!!!
    44.     void SetCountText()
    45.     {
    46.         countText.text = "Count: " + count.ToString ();
    47.         if (count >= 10)
    48.         {
    49.             winText.text = "YOU WIN!!!";
    50.         }
    51.     }
    52.     void SetTimeText()
    53.     {
    54.  
    55.         timeText.text = "Time: " + timelimit.ToString ();
    56.         if (timelimit >=40)
    57.         {
    58.             winText.text = "You lose...";
    59.         }
    60.         SetTimeText ();
    61.     }
    62. }
    This is basically the Playercontroller made in the tutorial. the i added the timeText and timelimit variable and tried to count the time since the games start, so i used time.fixedTime because in the documentation it is said that its the time passed since the start of the game. I used float because i couldn't use int with time.fixedTime and it told me that it was not able to convert int into float, so i changed it by hand. The if function basically says that if the time is over 40 it should display "You lose..." Now the timer does not work and over the player character there is always standing "New text".

    I hope i did not mess up completely because I'm a total beginner^^ Does anybody know how to do a timelimit properly and can tell me what i did wrong?

    Also, sorry for the bad english, its not my first language^^

    ~ SadoMessiah
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    The start function is only called once at the start of the script being started, so the functions you called will only be called once. Two functions that come in handy for game timers are coroutines and also InvokeRepeating. But to make it work as is, the set time function would need to be in fixed update or in the update. Normally, you use update for non physics related things.
     
    Blackthorn_01 likes this.
  3. Blackthorn_01

    Blackthorn_01

    Joined:
    Jan 28, 2015
    Posts:
    4
    Hey thanks. I will try that tomorrow^^
     
  4. Blackthorn_01

    Blackthorn_01

    Joined:
    Jan 28, 2015
    Posts:
    4
    It worked^^ Thread can be closed^^