|
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?
(comments are locked)
|
|
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: pls note that the line "static var myVar:int = 10;" should be declared globally in your javascript ( mean you declare it outside the functions )
Nov 21 '10 at 06:05 PM
denewbie
As for saving, you can either choose to save into xml or database or the asset server. It pretty much up to you.
Nov 21 '10 at 06:12 PM
denewbie
Thanks! I will give it a go right away!
Nov 21 '10 at 06:52 PM
OmegaVemon
(comments are locked)
|
|
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. I see, I think I'll have to check more on the PlayerPrefs and XML thingies. Thanks!
Nov 21 '10 at 06:55 PM
OmegaVemon
(comments are locked)
|
