Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roamcel · Aug 12, 2011 at 07:39 AM · variableprojectglobalreferencespersistent

project wide variables and constants

My project relies on a few scripts that need to be persistent throughout the whole life of the application, and their function is to basically hold the reference and values of gameobjects and states, so I was wondering:

 is there a way to create project wide variables that you can access without
 GetComponent and GameObject.Find?

I have just started thinking about the editor, but apart from the tutorials haven't seen much of them around:

 do you think that it's possible to create an "editor class"(<= totally guessing here) that will exist
 throughout the whole application, and which could replace my persistent scripts 
 in storing my variables, and that I could access without .Find?

My current scripts rely on dontdestroyonload and they work with it, but I am forced to perform many .Find and .GetComponent to get the references of my scene objects and variables (there's a lot of interaction in all scenes).

thanks

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

2 Replies

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

Answer by sven1994 · Aug 12, 2011 at 08:31 AM

You can have a class (doesn't have to derive from MonoBehaviour) with public static variables. You can then just access them from everywhere in your project with ClassName.variable, but I don't think you can assign static vars in the editor. Example:

 class MyVariableStorage
 {
     public static int importantVar = 0;
 }

 class AnyScript : MonoBehaviour
 {
     private void Start()
     {
         Debug.Log(MyVariableStorage.importantVar);
     }
 }
Comment
Add comment · Show 3 · 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 Kacer · Aug 12, 2011 at 08:35 AM 0
Share

Do remember though, when using statics, that they do not get reset when you load a level.

avatar image roamcel · Aug 12, 2011 at 09:52 AM 0
Share

Thanks a lot for the suggestion, but static offers a different approach to what I'm trying to achieve, since I'm actually trying to have these variables exposed somewhere that I don't need to instantiate or create as a gameobject in my scenes (I have a few classes that 'dontdestroy', but of course they only exist if I come from the scene they're generated into).

It is perhaps possible to mix the static variable definition with an editor override or new editor class? lol my brain freezes at this point... could I possibly add a new 'project settings' submenu, like 'global objects', which could open an ad-hoc menu in the inspector?

Any of you has any experience on editor tweaking to know if that's a doable solution?

avatar image Kacer · Aug 12, 2011 at 09:57 AM 0
Share

Singletons maybe?

Thing is, you can access statics from any script, even if they arent in the scene, usually i make a script with a load of statics in them if i want to save some data between scenechanges

avatar image
0

Answer by synapsemassage · Aug 12, 2011 at 10:59 AM

 public var myPublic : float = 0;//if no other value is assigned in editor  it will reset static variable to 0
 static var myStatic : float;
 
 function Awake()
 {
 myStatic = myPublic;
 }
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 roamcel · Aug 12, 2011 at 11:25 AM 0
Share

thanks but this is still a bit far from what I'm asking, since to expose myPublic you need to attach the script to a gameobject in the scene.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Global Varible Problem 2 Answers

Global Variables with Prefabs/ Self-Naming Variables 2 Answers

Howto access global var from other script? 2 Answers

Newbie questions! (Variables and Saving) 2 Answers

How do you achieve variables and functions that are global between scenes? What is the BEST way? 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