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 OmegaVemon · Nov 21, 2010 at 05:52 PM · variableloadingsavingglobalserialize

Newbie questions! (Variables and Saving)

Ok, I am pretty new to Unity free. So far, I am really liking the whole editor, it is pretty easy to use and understand. One of my favorite features would have to be, definitely, being able to expose variables in the editor!

I am using javascript for all my code needs (I'm also new with it but I'm having no big problems so far).

I am curious, is there some kind of way to create "Global Variables", that can be read and edited from absolutely anywhere in Unity? A value available in all scenes? Maybe I want to save the player's score.

Talking about saving, is there a tutorial about saving in Unity? Like, save variables values and then read them back later?

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
2
Best Answer

Answer by Eric5h5 · Nov 21, 2010 at 06:12 PM

Any public variable can be accessed from any object; see here. It's generally best not to use static variables unless you have a reason for it, since they have certain limitations (see the 6th post in this topic for a discussion of these), although a player's score is probably a good candidate. Only one scene at a time can be loaded, but you can use DontDestroyOnLoad in order to persist manager scripts across scenes. You can save and load variables using PlayerPrefs.

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 OmegaVemon · Nov 21, 2010 at 06:55 PM 0
Share

I see, I think I'll have to check more on the PlayerPrefs and X$$anonymous$$L thingies. Thanks!

avatar image
1
Best Answer

Answer by denewbie · Nov 21, 2010 at 05:59 PM

You can try using public static. Just attach them to the script and call them with the script's name. Then attach the script to a GameObject that can persist throughout all levels.

something like this.

In Javascript named "AppConfig"

static var myVar:int = 10;

In all other scripts you can call it in this manner: AppConfig.myVar

To allow it to persist in all scenes apply this on the GameObject the script is attached to:

// Make this game object and all its transform children
// survive when loading a new scene.
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
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 denewbie · Nov 21, 2010 at 06:05 PM 0
Share

pls note that the line "static var myVar:int = 10;" should be declared globally in your javascript ( mean you declare it outside the functions )

avatar image denewbie · Nov 21, 2010 at 06:12 PM 0
Share

As for saving, you can either choose to save into xml or database or the asset server. It pretty much up to you.

avatar image OmegaVemon · Nov 21, 2010 at 06:52 PM 0
Share

Thanks! I will give it a go right away!

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

No one has followed this question yet.

Related Questions

Saving Game Confussion 1 Answer

Save Player Health and stats 1 Answer

C# PlayerPrefs not Work 2 Answers

Best way to save data? (rpg game) 0 Answers

How do I save a list of scriptable objects? 0 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