Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I need help with a timer.

Discussion in 'Getting Started' started by Petras99, May 29, 2016.

  1. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I am making an obstacle course game where the player has to make it past the obstacles and to the finish line. I want it so while they're doing the level, it displays the time since level start in the top left corner. After they complete i would also like it to show the time it took for them to complete it. preferred language, C#.
    Any responses are appreciated!
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    What have you got so far?

    (I am assuming by "help" you meant, "I am stuck somewhere and I need some pointers" and not "someone do this for me")
     
  3. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    1. Code (csharp):
      1. publicGUIText timer;
      2. [*]publicfloat myTimer =0.0f;
      3. [*]voidUpdate()
      4. [*]{
      5. [*]myTimer+=Time.deltaTime;
      6. [*]timer.text ="Time :"+(int)myTimer;
      7. [*]
      8.  
      9. [*]}
      10. [/LIST]
    2. This is the code i have. I came upon this code by another forum, however i haven't been able to get this to work yet. Sorry for leaving this code out of the post.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,936
    That looks like it should work, what is the problem exactly?
     
  5. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I get an error saying "An object reference is required to access non-static member 'Timer.text'". I feel like this is going to be an easy fix, but, my brain isnt working right now.
     
  6. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I tried this code:

    Code (CSharp):
    1.     public GUIText timer;
    2.     public float myTimer = 0.0f;
    3.  
    4.     void Update ()
    5.     {
    6.         myTimer += Time.deltaTime;
    7.         timer.text = "Time: " + (int)myTimer;
    8.  
    9.  
    10.     }
    It appears to work.

     
  7. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    So my problem was actually with the way i was trying to show the text on the screen. I have tried a few different ways, but all of them give me an error. If you dont mind could you quickly explain how to send it the screen. I apologize for my ignorance i am fairly new to Unity. And thank you for your response!
     
  8. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    GUIText is part of the old UI system.

    So, in a sense, you shouldn’t be using it at all as essentially mentioned here:

    http://docs.unity3d.com/Manual/class-GUIText.html

    “This component relates to legacy methods for drawing UI textures and images to the screen. You should use Unity’s up-to-date UI system instead. “

    Here are some steps that should work though:

    1. Create an empty GameObject.

    (GameObject -> Create Empty)

    2. Add a GUIText component to it.

    (Component -> Rendering -> GUI Text)

    3. Type “hello” in the text field, and be sure you can see “hello” in the Game view. You might want to change the Anchor from “Upper left” to “Lower left” to make the text on the screen.

    4. Now select the object with your script attached to it. Drag that GameObject you created onto the “Timer” field.

     
  9. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    upload_2016-5-29_20-22-17.png

    I did exactly what you did but the "hello" isnt showing up??
     
  10. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I suspect it is because your X, Y, and Z on the GameObject are not all 0.

    Try typing 0 in for X, Y, and Z. Or, tell Unity to do a “Reset Position” like this:

    Screen Shot 2016-05-29 at 10.30.50 PM.png
     
  11. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I tried that but it isnt working :/
     
  12. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    It looks like its how my camera is set up.
     
  13. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I figured it out, i made a canvas with some GUI text and just changed a few things in the script. Thank you for your responses!