- Home /
Question by
Trollvahkiin · Sep 15, 2013 at 03:10 PM ·
guicolliderscoreitems
Adding 1 point after collison
Hey,
I have an objective in my game where you have to pick up 4 times and put them in a collider. The score is kept in the upper right corner and I don't know hot to only change the number in it and not the text every time another item is added. It look like this.
#pragma strict
var item : String = "items collected: 0"; //score kept in the upper right corner
function OnTriggerEnter(col : Collider)
{
if (col.tag == "item")
{
items = "items collected: 1"; //chnages the text to say 1 but I need it to simply add 1 every time an items is added
yield WaitForSeconds(1);
Destroy(col.gameObject);
}
}
function OnGUI () {
GUI.Label(Rect (10, 10, 130, 25), item );
}
Comment
Best Answer
Answer by dorpeleg · Sep 15, 2013 at 03:17 PM
What you need to do is, add a counter.
So:
var item : String = "items collected: "; //text
var score : int = 0; //counter
function OnTriggerEnter(col : Collider)
{
if (col.tag == "item")
{
score++; // add 1 to score
item = "items collected: " + score;
yield WaitForSeconds(1);
Destroy(col.gameObject);
}
}
Works! Thanks, I knew it was something to do with adding a counter but I didn't know it was going to be that simple! :)