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 Essential · May 28, 2014 at 06:46 PM · variableeditor-scriptingstatic

Setting static variables in Editor script

I need to set some variables in one of my runtime scripts, but I need to do it from an Editor script that is targeting a different script.

For example: DataScript + DataScript_Editor needs to set variables in RuntimeScript.

Can I do this simply having the RuntimeScript vars as static? Would assignment of static properties persist through playing & stopping the game, quitting Unity, all the way to publishing builds, and they'll never change or reset unless I explicitly reassign them? — Is this what static vars are for? — Or is there some conditions where a static var will be reset?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fafase · May 28, 2014 at 07:58 PM

Static is not meant to be a variable to live for the whole extend of a program, it just happens to do so but this is in no way why it was defined.

Static is a variable that belongs to the class while others are instance members. The reason why it lives through the whole project is because it belongs to the class and not to any instance objects. As a result whether there is an instance or not, the static is there since the class is just a mold for object and is always there.

Like the count of enemy is a candidate for static as you need to keep track of how many of them and if none left you still need that value for later when you create new ones.

Nonetheless, whatever you do with a static can probably be done without it.

Static is like any other variables, stored in the ram which is a volatile memory (values are just electronic impulse) so when you put your computer off all is lost. For this you need to store on the ROM via some text file or player prefs class from Unity.

Finally, static variable do not show in the inspector since they do not belong to the object but the class (I start to repeat myself). So the inspector do not show it.

Still it is possible to create a public instance variable that will set the static in the start or later. Other way is to use an Init(value) to set the static when needed.

Comment
Add comment · Show 5 · 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 Essential · May 29, 2014 at 12:04 PM 0
Share

Thanks for the clarification. I noticed static variables seem to persist between sessions but if that's not expected behavior, I obviously cannot rely on it.

I will find a workaround. Thanks!

avatar image fafase · May 29, 2014 at 12:56 PM 0
Share

it is expected behaviour, it is just not what it was designed for. The nature of static makes them persistent. The big thing is what are you after exactly and maybe static would be a candidate but it seems to be more of a external storage system.

avatar image Essential · May 29, 2014 at 02:44 PM 0
Share

I'm setting up my gameplay properties for each of my game's levels (e.g., time limits, enemy numbers, objectives, etc.), which is currently input and saved via the inspector of a 'LevelData' prefab object which I instantiate at runtime. Some properties are calculated and then populated automatically via an editor script. — This works great, but now I need to automatically adjust a property on an unrelated object. I could just reference this object from the original prefab, but I wondered if I could just set it via a static variable.

avatar image Kittymonkey · Feb 15, 2017 at 07:41 PM 0
Share

It is not because I want to say this is wrong, but if this is the case, why does static classes exist? (And sorry if this is a bit late to ask.)

avatar image Landern Kittymonkey · Feb 15, 2017 at 08:31 PM 0
Share

There are reasons and then there are reasons, $$anonymous$$icrosoft is good enough to detail their thoughts on the subject -> https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

avatar image
2

Answer by cryingwolf85 · May 28, 2014 at 06:58 PM

The actual use of static variables is a variable that extends across the entire run of the program. Whether you use a static variable or pass an object around has no effect on when the variable is removed from memory.

In your case using a static variable would be incorrect. When you stop running the program it will be removed from memory (as will any other type of variable).

I feel that the function DontDestroyOnLoad may be of use to you. As for going across builds, you can check out something like PlayerPrefs. You're going to need to save the data externally (Example: Database/file).

Good luck.

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 Essential · May 29, 2014 at 12:06 PM 0
Share

Thanks for the help. I picked fafase's answer as it went into a bit more detail about static vars but thanks for the suggestions. I think what I need to do is change the variables in the prefab, that way the values will stay set.

avatar image cryingwolf85 · May 29, 2014 at 09:09 PM 0
Share

That's fine, just glad you got it working.

avatar image
0

Answer by JohnPoison · Jun 21, 2016 at 09:27 PM

Currently, for storing data between editor sessions you can use ScriptableObject. Notice that you can't modify it in runtime inside a player, only in editor. But I think it fits to your requirements.

Comment
Add comment · 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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Static Variable Problem 1 Answer

Public and Static Variables 2 Answers

Is it possible to show Static Variables in the Inspector? 10 Answers

Increasing a score value? 2 Answers

Will static variables not work if negative? 1 Answer


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