- Home /
Updating GUIText
I'm trying to make a very simple Lap counter for a racing game.
As i'm posting a question here, you can prolly guess that i have a problem (you smart cookies, you!).
I have a GUIText called "Lap Text" which displays the players laps (this part is fine), it has the folowing code:
var Counter : int = 0;
function Update (){
Counter=0;
guiText.text = "Lap: "+Counter;
} The part i'm having trouble with is updating the 'Counter' variable when a trigger is passed through by the player. I'm currently trying a GetComponent JS script:
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Start"){
gameObject.Find("Lap Text").GetComponent(MonoBehaviour); MonoBehaviour.Counter +1;
}
} What's supposed to happen is that the player passes through the trigger (called 'Start' and the GUIText 'Counter' is increased by 1, but it isn't working.
Please tell me where i'm going wrong :(
Answer by aldonaletto · Sep 21, 2011 at 06:57 PM
You should use the script name instead of MonoBehaviour - unless the first script is called MonoBehaviour.js and is the only one with this name in your project - and assign the result of GetComponent to a variable of the same type (the script type is its name without quotes or extension), then use it as the script reference. Supposing the first script is called LapDisplay.js:
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Start"){
var scrpt: LapDisplay = gameObject.Find("Lap Text").GetComponent(LapDisplay);
scrpt.Counter += 1; // that's the way to increment the Counter variable
}
} NOTE: You may also notice some hickups when any car passes the Start - searching for some object among a thousand others may be too slow. The fastest way in this case would be to have a GUIText public variable and assign it at Start or at the Inspector instead of look for it every time a vehicle crosses the line:
var lapText: GUIText;
function Start(){ lapText = GameObject.Find("Lap Text"); }
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Start"){
var scrpt: LapDisplay = lapText.GetComponent(LapDisplay);
scrpt.Counter += 1; // that's the way to increment the Counter variable
}
}
Thanks for help AND the additional information (the 'function Start' bit.
Helped alot :)
Answer by FLASHDENMARK · Sep 21, 2011 at 07:04 PM
One similar but simple solution is to mark the variable 'counter' as static(which means that it can be accessed from all other scripts) and add to that variable. The only thing you need in order to access the variable is(to mark it as static of course) know the name of the script and the name of the variable you are accessing. So: nameOfScript.theNameOfTheVariable += 1;
//Call this script Counter
static var counter : int = 0;
function Update (){
guiText.text = "Lap: "+Counter;
}
//And you can call this script what you want
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Start"){
//Access the variable by the script name and the variable name
Counter.counter += 1;
}
}
NOTE: In your script you are calling "Counter = 0;" That will always result in the variable to be 0 and never change. You should call that in awake so when the game starts the variable will be 0.
Answer by FLASHDENMARK · Sep 21, 2011 at 07:04 PM
One similar but simple solution is to mark the variable 'counter' as static(which means that it can be accessed from all other scripts) and add to that variable. The only thing you need in order to access the variable is(to mark it as static of course) know the name of the script and the name of the variable you are accessing. So: nameOfScript.theNameOfTheVariable += 1;
//Call this script Counter
static var counter : int = 0;
function Update (){
guiText.text = "Lap: "+Counter;
}
//And you can call this script what you want
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Start"){
//Access the variable by the script name and the variable name
Counter.counter += 1;
}
}
NOTE: In your script you are calling "Counter = 0;" That will always result in the variable to be 0 and never change. You should call that in awake so when the game starts the variable will be 0.
Thanks for your help, got it sorted now!
Also thanks for letting me know about my 'counter = 0' gaff! :)
Your answer
Follow this Question
Related Questions
GUIText spawning when gameobject is destroyed. 1 Answer
accessing script components from external objects 2 Answers
How can I increase Health Value on trigger enter, to another game object? 3 Answers
Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object 2 Answers
Setting thrown object to kinematic after hitting target? 1 Answer