My scoring system doesn't work

I have made 2 separate scripts. I want to show user that the score is increased when he shoots a robot. Here is my code:

For displaying the score:
var LivesLeft: int = 5;
static var score = 0; // the variable score is supposed to be global now

function Update () 
{
	if (LivesLeft < 0)
	{
		print ("dead");
	}

}

function OnCollisionEnter (col: Collision)
{
	if (col.gameObject.tag == "Bullet")
	{
		LivesLeft = LivesLeft - 1;
	}
}

function OnGUI ()
{
	GUI.Label (Rect (10,10,250,70), "Lives Left " + LivesLeft);
	GUI.Label (Rect (20,20,400,80), "Score: " + score); // display score
}

for bullet:
var bullet: GameObject;
var location: Transform;
var explosion: GameObject;
var BulletSpeed = 5;

function Update () {
transform.Translate (Vector3.forward * BulletSpeed);
}
function Explode ()
{
		Instantiate (explosion, location.transform.position, location.transform.rotation );
		Destroy (bullet);
}

function OnCollisionEnter (col: Collision)
{

	if (col.gameObject.tag == "Robot") //if bullet collides with robot
	{
		score = score + 100; //score should be used, because it is global. When collision happens, 100 is added
	}
	Explode ();
}

It gives me an error message saying “Unknown identifier ‘score’”

static var score = 0;

Not properly defined.

static var score : int = 0;

If this doesn’t help, I’m going to guess that you need to change your script execution order.

If the static variable is coming up Unknown it is highly likely that a script is trying to access it before it has been declared. You need to make absolutely sure that the variable is created and initialised before anything tries to access it. Make the script run first in SEO or declare the variable in a script that you know runs before the rest.

You have two scripts.

That makes two different classes.

You declare score as static in the first class. That means that score becomes “kind of” global for all objects of that class.

However, you try to access it from the other class. That other class cannot access it just by simply using its name, because score belongs to another class.

To access it from the bullet class, and supposing that your first script is called ScoringSystem.js, replace the line that is giving you the error message in the bullet class with this:

ScoringSystem.score = ScoringSystem.score + 100;

However, beware. This allows just for one score in the game. You cannot have several simultaneous players with their own scores. So keep that in mind.