- Home /
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.
You might need to paste your entire Game$$anonymous$$anager script, if you're permitted to do so.
it is quite lengthy. but believe me, there is nothing else in there affecting this variable. I have tried several ways to write this code. I think I may have a solution, but I would at least like to educate myself on WHY this strange behavior happens. Unity is a little different from JavaScript or ActionScript(EC$$anonymous$$A). So I have to un-learn a few things and learn gracefully new ones. lol. Thanks for your help, Tylo.
do you have @script ExecuteInEdit$$anonymous$$ode() anywhere? if so the script never stops running which is why your variables don't reset. You'll find that web and stand alone builds don't have this problem
That's exactly why I wanted him to post his entire script, spinaljack. Incase something like that was going on.
Oh, no. nothing like that at all. I don't even know what that is. Since I'm new to Unity3D my coding isn't best practice right now. And there's a lot of code all over the place. I've been trying to clean it up. But if I tell u that the rest of the code has absolutely no effect on this variable, you should believe me. Save yourself the mental anguish. lol. Thanks, Friends.
Answer by jashan · Aug 28, 2010 at 07:05 PM
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 ;-)
Thanks, Jashan. I've already tried using the variable as private and it does the same thing. I've watched the private variable by using debug mode. As far as the whole JavaScript thing, I'm really new to Unity3D, it's been about 3 weeks, so thanks for the info. It does say "JavaScript" though when creating the file in the Assets menu. Thanks, friend.
Answer by Antibiotix · Apr 28, 2013 at 04:56 PM
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. :)
just to add on to my previous comment, the line : var refToScene$$anonymous$$anager:script_Scene$$anonymous$$anager; opens up the variable in the inspector so that I can drag and drop the Scene $$anonymous$$anager gameobject to it.
I dragged and dropped the prefab version. This is because Unity does not allow me to drag and drop the instance from my hierarchy. Not sure is this is part of the cause...
While this is vageuly related, this is a new problem Antibiotix. I would post a new Question.