How to create gui text in c# which show time in race game?

Hi everybody

I tried many tutorials, I read documentation and I still don’t know how to create guit text in c# and update it. This is my piece of code.
Some lines are commented because don’t work.

using UnityEngine;
using System.Collections;

public class StageManager : MonoBehaviour
{
	//public Transform birdsPrefab;
	///float birdTimer;
	public int lapsNumber=2;
	public float startTime=0;
	public float stageTime=0;
//    GameObject StageTimeText = new GameObject("GUIText");
//GameObject ScoreText = new gameobject("score text");


	void Start()
	{
//stageTimeText = new GameObject("stageTimeText");
//stageTimeText.AddComponent(typeof(GUIText));
startTime = Time.time;
	}
	
	void Update()
	{
	stageTime = Time.time - startTime; 
	 //GUI.Label(new Rect(10, 10, 150, 150), "stageTime");
	}
	
    void OnGUI() {
        GUI.Label(new Rect(10, 10, 100, 20),"Hello World");
    }

}

At this moment I can just create programically label with text hello world :). But don’t know how to update it and how to show text from variable.

Let’s try this

using UnityEngine;
using System.Collections;

public class StageManager : MonoBehaviour
{
	public int lapsNumber=2;
	public float startTime=0;
	public float stageTime=0;


	void Start()
	{
startTime = Time.time;
	}
	
	void Update()
	{
	stageTime = Time.time - startTime; 
	}
	
    void OnGUI() {
         GUI.Label(new Rect(10, 10, 150, 150), stageTime.ToString();
    }

}

There is how to draw a variable in a GUI element :slight_smile:

Hope that helps
Myhi

Thanks Myhi
I thought I’ve to change int to str. But I’ve another question. How to assign this to variable. I think about this

public totalTimeText (I don’t know what type it should be) and then
totalTimeText = GUI.Label(new Rect(10, 10, 150, 150), stageTime.ToString());

I ask about that because sometimes when some event occur I want to hide this or maybe change collor. Sholud I move all gui text to another script and all this work in that way that everything is drawning from start on every frame ?. If you add gui text by editor you have existing object to which, if you add this in code like above how for example call this label from another script. I know that in javascript you can create public variable then drag in editor gui text and assingn this gui text to that variable in editor. I want do this all programically in c#. I hope I write it clearly.

Ahh I totaly missuderstand onGui function. It refresh on every frame so on every frame I draw those box form start so all logic,if statemets for gui and etc, I should have in this function.

If you want a timer in a GUI wndow, you could do something like this:

using UnityEngine;
using System.Collections;

public class TimerScript : MonoBehaviour 
{

	public float timer = 0.0f;
	public bool timerStarted = false;
	
	void Update()
	{
		if( Input.GetKeyDown( KeyCode.N ) ) 	// Use the N-key to toggle the timer On and Off
		{
			if( timerStarted == false )
				timerStarted = true;
			else
				timerStarted = false;
		}
		
		if( timerStarted )
			timer += Time.deltaTime;
	}
	
	void OnGUI()
	{
		GUI.Label( new Rect( 40, 40, 120, 50 ), "Timer: " + timer );
	}
	
}

Me and my partner are making a game for a project in our computer programming class, we are using C# in the unity editor. Its a racing type game. We already have the map but, we need a timer, I see this but how do I actually put a timer on the screen? Please email me at 3512664784@r9tigermail.org that way i will actually get a notification when someone replies. Thank you!

Yeah no one’s gonna email you, this reply will have to do.

You’re commenting on an old post, which uses Unity’s old UI system - while it works, it’s far inferior to the current system. Watch the Unity UI tutorials and, if you need help after watching the tutorials, start your own thread instead of necroposting on a 3.5-year-old one.