Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by uni · Aug 28, 2010 at 04:22 PM · inspectorvariablesincrementscopeinitialize

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tylo · Aug 28, 2010 at 04:24 PM 0
Share

You might need to paste your entire Game$$anonymous$$anager script, if you're permitted to do so.

avatar image uni · Aug 28, 2010 at 04:31 PM 0
Share

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.

avatar image spinaljack · Aug 28, 2010 at 06:34 PM 0
Share

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

avatar image tylo · Aug 28, 2010 at 07:16 PM 0
Share

That's exactly why I wanted him to post his entire script, spinaljack. Incase something like that was going on.

avatar image uni · Aug 28, 2010 at 07:25 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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 ;-)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image uni · Aug 28, 2010 at 07:18 PM 0
Share

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.

avatar image
0

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. :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Antibiotix · Apr 28, 2013 at 03:39 PM 0
Share

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...

avatar image Thom Denick · Apr 28, 2013 at 05:54 PM 0
Share

While this is vageuly related, this is a new problem Antibiotix. I would post a new Question.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Variables assigned in inspector disappear in git version-control? 1 Answer

Class functions don't seem to be updating class variables 1 Answer

Can't See variables in inspector 2 Answers

Variables having 2 values within a Class? 0 Answers

Inspector slider value? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges