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!
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”)
publicGUIText timer;
[]publicfloat myTimer =0.0f;
[]voidUpdate()
[]{
[]myTimer+=Time.deltaTime;
[]timer.text =“Time :”+(int)myTimer;
[]
[*]}
[/LIST]
- 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.
That looks like it should work, what is the problem exactly?
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.
I tried this code:
public GUIText timer;
public float myTimer = 0.0f;
void Update ()
{
myTimer += Time.deltaTime;
timer.text = "Time: " + (int)myTimer;
}
It appears to work.
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!
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:
- Create an empty GameObject.
(GameObject → Create Empty)
- Add a GUIText component to it.
(Component → Rendering → GUI Text)
-
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.
-
Now select the object with your script attached to it. Drag that GameObject you created onto the “Timer” field.
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:
I tried that but it isnt working
It looks like its how my camera is set up.
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!