- Home /
int +=1 won't exceed 1
Hello there, I'm having problems with my score script for my game The score won't ever exceed 1,
It's supposed to add 1 to the score gui on Trigger enter, i have the code compounded on the coin itself
The update from 0 to 1 works but no matter how many coins i pick up it always stays 1 The Debug.Log was simply to test if it was the GUIText screwing up, but it has to be the integer... Here's the code
#pragma strict
public var score : int = 0;
var guiScore : GUIText;
function Start ()
{
guiScore.text = "Score: 0";
}
function OnTriggerEnter (info : Collider)
{
if (info.tag =="Player")
{
Destroy(gameObject);
score += 1;
guiScore.text = "Score: " + score;
Debug.Log("The value is: " + score);
}
}
$$anonymous$$aybe its a problem that you destroy the gameObject ?
I tried it with destroying the object after adding the score but that didn't work out either
Just tried getting rid of the destroy completely and that worked, so it must be destroying the object before executing the script properly
Answer by meat5000 · Nov 20, 2013 at 05:09 PM
You Destroy the gameobject the score modifier is attached to. It will work once then be destroyed.
As score is a variable you create in that script it too will be destroyed.
Make your score a variable on some non destroyed object and use GetComponent to modify it instead?
var scoreboard : GameObject; //for instance of scoreboard object
scoreboard = GameObject.Find("ScoreBoard"); //Link instance to the actual ScoreBoard object you place in hierarchy, Performed in Start()
scoreboard.GetComponent(Score).score += 1; //line used to modify the score.
Create a ScoreBoard gameobject in hierarchy and place a script on it with a
public var score : int = 0;
Oh, yeah that sounds like it should work.. Thanks a bunch!
Answer by aldonaletto · Nov 20, 2013 at 05:18 PM
Is this script attached to the coins? If so, the problem is the score variable: there exists one score in each coin instance, thus the coin hit increments its own score and dies. A simple solution is to declare score a static variable:
public static var score: int = 0;
Static variables are created when the game starts and survive til its end. They are the closest equivalent of global variables in other systems (a traditional global variable isn't only globally accessible, but also lasts for the whole application life).
A more versatile alternative is to declare score in the GUIText script, and modify it via SendMessage. The GUIText script could be as follows:
public var score: int = 0;
function Start(){
AddScore(0); // force text to update to current score
}
function AddScore(points: int){
score += points;
guiText.text = "Score: "+score;
}
Just call AddScore via SendMessage in the coin script:
var guiScore : GUIText;
function OnTriggerEnter (info : Collider){
if (info.tag =="Player"){
// call AddScore(1) in a script attached to guiScore:
guiScore.SendMessage("AddScore", 1);
// suicide coin:
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Uniting 2 integers from two different sources 1 Answer
How do i link a GUI text to a variable text which is a integer? 2 Answers
Score Counter 2 Answers
Keeping Score registering more than one Point sometimes on Destroy 2 Answers
GUI Text Score counter 3 Answers