- Home /
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'"
It's missing some script parts formated as code, can you please put the entire error message and edit your question?
What happens if you delete the static front of that score?
that's all i have for scoring system, and i thought that static is supposed to make it global
Wait a $$anonymous$$ute! You are using two different scripts right? And score is only defined in one script and you are trying to get the variable in one of the script to another right?Well you didn't do anything to call it?
Answer by meat5000 · Nov 05, 2013 at 07:23 PM
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.
Would it make a difference? Its a float as he wrote down.
I'm betting, yes. As his second script is clearly having trouble accessing his 'static' variable.
Also, I don't see the word float anywhere.
Oh i knew that as ive learned from the tutorials. As long as you dont specify what it is like is it a float or int? So var number = 5; is a float number. We didn't define that it is float but unity will understand any number as float if we dont specify that it is a integer Anyway this isnt the answer of the question or anything though.
Aye, an integer in JS is actually a parsed float. But I imagine with a static variable it'd be important to declare type. If it is accessed by multiple elements this seems important.
Answer by Azrapse · Nov 08, 2013 at 10:26 AM
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.
Don't bother, I gave that answer 5 days ago he did not bother trying. He does not want information, he wants solution so just copy paste his script with the correction if you hope for some recognition.
look, you did not understand, I tried all of them the instance you posted the comment, but i just ran into more trouble. I am a high school student, not a professional,and i have been trying to solve this problem for 2 weeks now.