|
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:
I then update the score from another object (the popping bubbles) like this:
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.
(comments are locked)
|
|
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:
Not sure how the exact JavaScript syntax is, but try something like:
or maybe just
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
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.
Aug 28 '10 at 07:18 PM
uni
(comments are locked)
|
|
Hi, I am facing a similar problem, Here are my scripts "scriptBullet.js" inside a gameObject "prefabBullet" _____________________________________________________________________________________ pragma strict
"scriptSceneManager.js" inside gameObject "prefabSceneManager" ______________________________________________________________________________________ pragma strictvar 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 refToSceneManager:script_SceneManager; opens up the variable in the inspector so that I can drag and drop the Scene Manager 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...
Apr 28 at 03:39 PM
Antibiotix
While this is vageuly related, this is a new problem Antibiotix. I would post a new Question.
Apr 28 at 05:54 PM
Thom Denick
(comments are locked)
|

You might need to paste your entire GameManager 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(ECMA). So I have to un-learn a few things and learn gracefully new ones. lol. Thanks for your help, Tylo.
do you have @script ExecuteInEditMode() 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.