Wow! Unbelievable Variable behavior acts static when it's not

Not sure what's going on but I have a variable, SCORE, in my GameManager script inside my Game Manager prefab. it's initialization is simple:

var score:int = 0;

I then update the score from another object (the popping bubbles) like this:

gm:Transform; //I drag the Game Manager prefab clone here
gm.GetComponent(GameManager).setScore(); //which simply does score++, period

problem is (you're gonna laugh your head off) when I stop the game the score stays the same in the Inspector and keeps the value when I restart the game. How trippy is that?

Can anyone tell me how much of a doof I am and what I need to do to reset the score, please? btw, I have set the score =0; at like 10 places in the init script and it stiiiiil starts at the value in the Inspector. ha ha. I have to manually set it back to 0 everytime. Thanks in advance, friends.

In general, it's not a bad idea to reset the scores when the game starts, e.g. during Awake() or Start(). I wouldn't say your variable acts like a "static" variable - that would mean that you have the same score in each instance of your script (if your script is attached to multiple game objects).

Also, you might not really want to put the score into a public variable, which you are actually doing when you write:

var score:int = 0;

Not sure how the exact JavaScript syntax is, but try something like:

private var score:int = 0;

or maybe just

private score:int = 0;

My guess would be that for some reason, mistakenly the value gets serialized which makes it persistent (it shouldn't do that while playing, though). By declaring it private, it won't be made persistent anymore (you also will no longer see it in the editor, though).

You can still access the variable from outside by providing an accessor method (something like

function getScore() { 
    return score;
}

Finally, UnityScript has little to do with JavaScript (except for the syntax which should be pretty much exactly the same ... mostly ;-) ). It's kind of like JavaScript has little to do with Java - the only reason JavaScript is called JavaScript was because at that time, Java was pretty hip on the web ;-) ... that's the reason, why I don't really like UnityScript and prefer C#: C# is C# is C#, no matter whether it's in Unity or anywhere else ;-)

Hi, I am facing a similar problem,

Here are my scripts

“script_Bullet.js” inside a gameObject “prefab_Bullet”


#pragma strict

var bulletSpeed:float = 50;
var refToSceneManager:script_SceneManager; // set a reference to script_SceneManager

 
function Update () 
{
transform.Translate(Vector3.up* Time.deltaTime* bulletSpeed);
}


function OnTriggerEnter(other:Collider)
{
	if(other.gameObject.tag == "Asteroid")
	{	
	    
	    refToSceneManager.addScore();
	}
}

“script_SceneManager.js” inside gameObject “prefab_SceneManager”


#pragma strict

var canInstatiateAsteroid:boolean = true;
var minTime :float = 3;
var maxTime :float = 6;
var refToAsteroid :Transform;
var score :int = 0;
var gameTime :float = 60.0; // countdown timer to game ending

function Awake()
{
score = 0;
}

function Update ()
{
if(canInstatiateAsteroid == true)
{
instantiateAsteroid();
canInstatiateAsteroid = false;
}
}

function instantiateAsteroid()
{

GameObject.Instantiate(refToAsteroid,Vector3(0,-9,0),transform.rotation);
yield WaitForSeconds(Random.Range(minTime,maxTime));
canInstatiateAsteroid = true;

}

function addScore()
{
score++;

}


What I’ve realized is that at start of the scene, the score inside my SceneManager instance gets reset to zero, but the score inside my SceneManager prefab retain the value from previous run.

And when I start the scene again, score will be assigned the value of the prefab version ( which is whatever value retained from previous run) and not the instance version ( which has been reset to zero).

Why is the prefab score being changed? Shouldn’t it be just the instance that is affected?
Why is the prefab score being used? Not the instance score?

Would be grateful if anyone can shed some light on this. :slight_smile: