- Home /
Increasing a score value?
Hello, this is the script for a projectile in my game. I want to make it so that when Object A collides with an object with tag B the score is increased, to put it simply, why doesn't this work?
> function OnTriggerEnter (other :
> Collider) {
> if (other.gameObject.CompareTag ("Wall"))
{
Destroy (gameObject);
};
if (other.gameObject.CompareTag
<p>("Enemy")) {
Destroy (gameObject);
Destroy (other.gameObject);
{
score += 1;<br>
if (score == 5)
{
displayScore(score);
}
};}
function displayScore(int score)</p>
<p>{ GUI.Label(Rect(0, 0, 50, 50),
score); }</p>
The indentation and formatting of your post is all messed up, I unfortunatly don't have the permission to edit it for you so I would kindly ask you , or another with the permission to do so. And no I sadly don't have the answer for you
Answer by Bravini · Nov 18, 2010 at 11:25 AM
static var Score : int;
now you can access this variable from any script by using scriptname.Score, being scriptname the name of the script you declared the variable into.
also, if your score var is in a script attached to the projectile, you gonna have multiple scripts in the scene, which will be destroyed along with the projectile when they collide. Put the score in the player or any other object you know that wont be destroyed.
other solution would be to use http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
Answer by jameslee0227 · Nov 18, 2010 at 08:08 AM
do you want show score?
recommand below
{
int score = 0;
function OnTriggerEnter ( )
...
score++;
...
}
void OnGUI() { if (score == 5)
{
GUI.Label(Rect(0, 0, 50, 50), score);
}
}
}