- Home /
Point System. GUI Help.
Hello, So I have this "coin" and it floats. When Player picks up this coin, PlayerScore += 1. This all updates but I would like to have a GUI Text update. So that when the player picks up this "coin" it updates + 1. *I would like the GUIText named collectablewall01. Here is my code:
var PlayerScore : int; var ScoreText = "Score: 0"; var MaxPoints : int;
function OnTriggerEnter(other : Collider){ if(other.tag == "Point"){ PlayerScore += 1; ScoreText = "Score: " + PlayerScore; Destroy(other.gameObject); } }
function Update(){ if(PlayerScore > MaxPoints){ PlayerScore = 0; print("MaxReached"); ScoreText = "Score: 0"; } } The code is on the Player. Please help, thank you,
Just add an OnGUI function. I've predefined a Rectangle which is (xPosition, yPosition, width, height). I put the Rect default for your scoreText as (0, 0, 256, 128) this will put it 0 at x, 0 at y, 256 width max, and 128 height for the rectangle the text will appear in. You don't actually see a rectangle, this is just the area, which you can adjust in the inspector. I've added an option GUIStyle, where as you can adjust color, style, font size, change font, etc.. in the inspector. If you want to change the color in the inspector, remove the style.normal.textColor line from the Start() function. Simply replace your code with this:
static var PlayerScore : int;
var ScoreText = "Score: 0";
var $$anonymous$$axPoints : int;
var ScoreTextPosition : Rect = Rect(0, 0, 256, 128);
var style : GUIStyle;
function Start()
{
style.normal.textColor = Color.white;
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Point")
{
PlayerScore += 1;
ScoreText = "Score: " + PlayerScore;
Destroy(other.gameObject);
}
}
function Update()
{
if(PlayerScore > $$anonymous$$axPoints)
{
PlayerScore = 0;
print("$$anonymous$$axReached");
ScoreText = "Score: 0";
}
}
function OnGUI()
{
GUI.Label(ScoreTextPosition, ScoreText, style);
}
Note: Simply use this script the same way you did your old text. There is no need to attach a GUIText component from the menu. OnGUI function will update automatically with the score.
Answer by aldonaletto · Dec 16, 2012 at 04:19 AM
You should place a reference to the GUIText object in this script, and assign it in the Inspector, then in your script refresh the GUIText in Update:
var PlayerScore : int;
var ScoreText = "Score: 0";
var MaxPoints : int;
var gText : GUIText; // drag here the GUIText from Hierarchy
function OnTriggerEnter(other : Collider){
if(other.tag == "Point"){
PlayerScore += 1;
ScoreText = "Score: " + PlayerScore;
Destroy(other.gameObject);
}
}
function Update(){
if(PlayerScore > MaxPoints){
PlayerScore = 0;
print("MaxReached");
ScoreText = "Score: 0";
}
gText.text = ScoreText; // update GUIText each frame
}
But when the next level is loaded the score-board will toggle and starts from "0". Can you tell how it would continue adding points for next levels too?
$$anonymous$$y script is:
var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;
function OnTriggerEnter(other : Collider ) {
if (other.tag == "Pickup") { //Pickup is the tag please replace it with your pick-able item tag
Debug.Log("Other object is a coin");
score += 1; //You can replace 1 with any integer this is you score
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (200, 10, 200, 400), scoreText.ToString());
}
If your game has one only score variable, declare it as static: static variables are created when the game starts and survive to level loadings:
static var score = 0;
...
The initialization of score is done only once, when the variable is created, thus it will keep its value across all levels.
NOTE: A static variable is unique - if there are several instances of the script where it's declared, all of them refer to the same variable. Using a static variable for the enemy health, for instance, is a disaster: when one dies, all of them drop dead at the same time, because the health value is the same!