- Home /
Trying to get score counter...
I got my score counter to show up on my GUI Text, yet when I switch to another object with the same script its resets the score to zero. What can i change or add to stop it from resetting?
var speed = 5;
var buck = 100;
var deer = 10;
private var score : int = 0;
var ScoreBoard: GUIText;
function Update () {
transform.position += Vector3.forward * Time.deltaTime * speed;
}
function OnMouseEnter () {
Debug.Log("Hit");
speed = 15;
deer += 10;
ScoreBoard.text = ("Score: ") + deer;
}
function OnMouseExit () {
Debug.Log("Safe");
speed = 5;
}
What do you mean, 'switch to another object'? I wasn't aware that moving components around was exactly straightforward. If you mean putting another instance of the script on a different object, then of course the score will reset to zero- it's a private member of a different object!
Answer by Holynub · Sep 22, 2011 at 02:18 AM
Wait. Are you attaching the same script to multiple objects? Because if you're doing that, this will treat each 'val deer' as different ones.
You'd need to setup a control script that acts as a global counter. This controller script can handle things like score keeping.
Answer by aldonaletto · Sep 22, 2011 at 02:12 AM
You can declare score as a static variable:
static var score : int = 0;
A static variable is created once at the program beginning, lives while the program is running and is unique: all script instances use the same variable. Non-static variables exist in each script instance and are independent of each other.
Your answer
Follow this Question
Related Questions
unknown identifer explosion problem 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Connecting 2 scripts(javascpt)? 2 Answers
problem with my simple script please help. 2 Answers
what is the problem of this code ? 2 Answers