- Home /
Help me with Gui Text
OK so here's the thing, I have some gameObjects when collided with the player equal to 10 points and the other 20.
But the problem is that can't get it to update/add the score.
It will do the 10 points but when I hit the 20 it does not do anything to the Gui text( though it does debug...)
Heres the code can someone help me figure out what is wrong?
P.S only difference between 10 point and 20 Point code are the points themselves.
var label : GUIText;
var Counter ="";
var Score : String = "Score:";
function Update() {
label.text = Counter;
}
function OnTriggerEnter(){
Counter = Score +=10 ;
Debug.Log("Get 10 Points!");
}
Sorry if i stuffed up the code part i could not get it working properly
Answer by Seth-Bergman · Aug 06, 2012 at 02:41 AM
for starters,Score += 10 isn't right..
say :
Score + 10;
but this will just print "Score:10" every time, if you want to accumulate points, you need an int to store the current score:
var label : GUIText;
var Score : String = "Score:";
var currentScore : int;
function Update() {
label.text = Score + currentScore;
}
function OnTriggerEnter(){
currentScore += 10;
Counter = Score + currentScore ;
Debug.Log("Get 10 Points!");
}
of course, each object with this script has it's own instance of currentScore, so whichever one hits will have its own unique score.. if you want a unified score, you would access a static var:
on player script (named "PlayerScript"):
var label : GUIText;
var Counter ="";
var Score : String = "Score:";
static var currentScore : int;
function Update() {
label.text = Counter;
}
on trigger script:
function OnTriggerEnter(){
PlayerScript.currentScore += 10 ; // use name of script
Debug.Log("Get 10 Points!");
}
hope this helps EDIT: ok, let me try to clarify:
first off, my mistake, I had put the "static" on the wrong line, it's fixed now; the point is, you need to declare an int (or float) to store numeric data, rather than a string; by declaring the int as static, we are saying this value is universal through all instances of this script. So, if I change it in one place, it changes everywhere. Of course, this won't matter much if the script in question is only used once, and normally it would be just once, on the player for example, which is why I did it that way.. BUT, just declaring it right in the same script would still keep it constant (if static) either way..
var label : GUIText;
var Counter : String;
var Score : String = "Score:";
static var currentScore : int;
var myDamage : int = 10;
function Update() {
label.text = Counter;
}
function OnTriggerEnter(){
currentScore += myDamage;
Counter = Score + currentScore ;
Debug.Log("Get " + myDamage+" Points!");
}
here the score is instanced in each object, but the overall score stays static between all objects (if I add 10, it updates every object to the same value)
which is basically what you want I think, just set the var myDamage to 20 on the other object prefab only prob is for performance, there's really no need to be updating the GUIText from EVERY object with this script(though I think it should still work though)..
Better to have one script somewhere drawing the GUIText, and access the int from the script with multiple instances, so in one script on ONE gameobject in the scene:
var label : GUIText;
var Score : String = "Score:";
static var currentScore : int;
function Update() {
label.text = Score + currentScore;
}
now since currentScore is static, we don't even need an instance of the script to access it, we can just refer to it directly using the name of the script:
var myDamage : int = 10;
function OnTriggerEnter(){
OtherScriptName.currentScore += myDamage;
Debug.Log("Get " + myDamage+" Points!");
}
this should work as seen here :)
Im afraid this is confusing me more, i tried the first script box and seem to do much the same as my script was doing
The are second and third script box are they part of the first or separate. the "player script" is it supposed be attached to the player gameObject or something else entirely. I'm guessing the "trigger script" is any gameObject that want to give points.
But when I tried the first script original i had to take out change the "static var" to a normal one or else i got this really weird error "BCE0135: Invalid name:script name".
Please write again, Still confused but also understand it a bit better...
ok, edited my answer, hope that clarifies..
sorry had static on the wrong line before, my mistake
edit to the edit: just re-edited again to be even clearer, the example there SHOULD work now, I think
Almost but not quite... Im still get the error BCE0135: Invalid name: '10 Pointer'
10 Pointer being the script name, and error is occurring on both this line: "currentScore += myPoints;" and this line... "Counter = Score + currentScore;"
It really is hating on that static var.... (I changed the myDamage to myPoints)
Answer by DNP · Aug 06, 2012 at 04:46 AM
var gText1: GUIText;
var gameHolder : GameObject;
function Update(){
gText1.text = gameHolder.GetComponent(NewerGameScript).totalNotebooks.ToString();
}
This is a shortened part of the code i use for my game. And, Yes it works. There should be no problems with this.
Your answer
Follow this Question
Related Questions
Trigger collision with Player won't work 2 Answers
Help with making a triggered gui message 1 Answer
Display a Text/GUI on screen when triggerd with Fadein/Fadeout 1 Answer
[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit? 1 Answer
Using a trigger to make a scrollview appear and then disappear? 1 Answer