- Home /
Adding a value after a trigger on
I am making a race car and I just want to add a value on my variable "Lap" once the car triggered a gameobject which act as my finish line
Simply i just want to do this
var lap = 0;
function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "Player") { lap +1; } }
then afterward i want to use the said value (lap) to show on my GUI and if the value is equals to 3 show a "Complete Race" text on my GUI
I used
var lapCounter : GameObject;
function OnGUI () { var curLap = lapCounter.GetComponent("lapCounter").lap; GUI.Label (Rect (Screen.width-100,50,120,20), "Lap:" +curLap);
and
var playerlap = 1; private var guiEnable = false;
function OnGUI() { if (guiEnable) { GUI.Label (Rect (Screen.width/2,Screen.heigth,400,200),"RACE COMPLETE"); } } function Update () { if (playerlap == 3) { guiEnable = true; } }
I don't know what seems to be the problem...
First off, has anyone noticed that posting code here just sux when it comes to formatting?
You're not really going into detail about the problem you're having @justify. Is nothing showing up for you. Can you also be a bit more specific which component is which, and possibly clean up your code pastes a little bit?
$$anonymous$$y comment was not against justify, but supporting justify and the difficulty posting code on Answers. Pasting code here is a crap shoot whether it will format at all. One gets a funny "paste code here" marker and more than 75% of the time it just looks like spilt garbage when it's posted.
$$anonymous$$ostly for justify: http://answers.unity3d.com/questions/165232/best-practices-for-posting-code.html
To fix your code, it seems the best practice is to fix it in a text editor, paste it, and then format it.
Answer by Adam-Buckner · Sep 08, 2011 at 08:58 PM
I would approach this by having the lap counter variable in the main script, and have the lap counter trigger set it.
On your trigger you could have:
var myGameManager : GameManager;
function OnTriggerEnter(other : Collider) {
if (other.gameObject.tag == "Player") {
myGameManager.lap +1;
}
}
(Hate this lack of code formatting...pasted it:) http://pastebin.com/DgMYeYgV
Then I would populate that slot in the inspector by grabbing your GameManager Object with the GameManager script on it and dragging it into the slot on the trigger.
This way the GameManager script do all of the manipulation and heavy lifting with a local value. With:
var lap : int;
... in your main GameManager, having Update () and OnGUI() doing the work should be seamless.
Your answer
Follow this Question
Related Questions
enable a canvas on trigger 2 Answers
Trigger 'if' statement while in TextField 2 Answers
How to have OnGUI element in a OnTriggerEnter function? 3 Answers
OnGUI not working? 1 Answer