- Home /
how GUIText get visible?
Hi guys, i'm creating racing game, so i created Lap Time Point, so i want when my car hit tiggered collider show the GUIText, i tried like this. sry for my bad english
var points = 0;
var targetGuiText : GUIText;
var Renderer;
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "FinalLap")
{
targetGuiText.enabled = true;
Finish();
}
}
function Update()
{
targetGuiText.enabled = false;
targetGuiText.text = ("I finished in: " +points);
print("I finished in: " +points);
}
function Finish()
{
if(Time.time <= 100) //if you make it before 90 seconds
{
points += 5; //add points
}
if(Time.time <= 50) //if you made it before 50 seconds
{
points += 10; //add points
}
}
Answer by RodrigoSeVeN · Aug 04, 2012 at 07:19 PM
The script seems to be in order, but you have one line that is not working as intended.
On update:
targetGuiText.enabled = false;
This line is repeating every frame, and when you detect the collision, this line won't let it become visible. Try to put this line inside function Start(){}
so it becomes invisible on the start and can become visible again when the collision is detected at the end.
Answer by Akill · Aug 04, 2012 at 07:12 PM
Check this : http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html
You want to keep track of time yourself, to do this is quite easy. Each time Update is called within Unity, the Time.deltaTime variable gets set, which is the time between each frame(in seconds).
So, if you keep a variable which will increment based on this value every frame, you can accurately test how long something has taken etc.
var points = 0;
var targetGuiText : GUIText;
var Renderer;
var raceTimer = 0;
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "FinalLap")
{
targetGuiText.enabled = true;
Finish();
}
}
function Update()
{
raceTimer += Time.deltaTime; //keep adding the time each frame
targetGuiText.enabled = false;
targetGuiText.text = ("I finished in: " +points);
print("I finished in: " +points);
}
function Finish()
{
if(raceTimer <= 100) //if you make it before 90 seconds
{
points += 5; //add points
}
if(raceTimer <= 50) //if you made it before 50 seconds
{
points += 10; //add points
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613075158im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Invisible objects renderer disable? How to stop this? 0 Answers
Mouse over make object Visible 2 Answers
get object invisible for couple seconds and get visible again 1 Answer
Converting C# to JS: rendering a mesh 1 Answer
Simple Point system 3 Answers