If I have the following code:
Can I use this to keep score that's available in all my scenes? If so then do I have to make an empty gameobject in each scene and then just attach the script to that or is there more I have to do?
asked Sep 26, 2012 at 06:42 AM
Sure! I am using the same methods in my own project right now.
First let me say that a guy named Petey has a youtube channel called BurgZergArcade that has some really awesome Unity3d C# programming videos!
That channel is here: http://www.youtube.com/user/BurgZergArcade
Anyway what I usually do that I learned from Petey is make a new empty gameobject and name it GameMaster, then make a C# script named GameMaster.cs.
There's at least two ways to do this. Here's the first way:
This is the quick and easy way to access variables like 'score' from any class. To do this just call GameMaster.score from anywhere. The problem with this method is that it only works well for simple data types like int. Most of the time we want to drag and drop in Assets via the inspector, and this isn't allowed with static variables.
In this case we create what's called a singleton. This is something Leepo from M2H uses in his multiplayer tutorials. We create a regular class and then make one single static instance of the whole class. This is the second way:
The beauty of using the singleton is that it can use the inspector to assign public assets and can still be used anywhere like a static class, even the functions will work from anywhere. We'd call the score for example by using GameMaster.GM.score
Of course there are many other ways to accomplish this, but this is how I like to do it. Hope that helps!
answered Sep 26, 2012 at 10:56 AM
The way I do it is that I have a normal variable (I call it temp) that I use in the scene and a static variable (static) that I use for the whole game.
The reason is, if you use the static var for your point and the guy loses, when he starts again, he still has the point from previous run.
You could use temp and if the guy wins the level then
If the guy loses
Temp is lost when the new level is loaded, static remains. You can attach the static var to your player object. The static var will have a lifetime of the whole game and you can access it anywhere with ClassName.staticVar.
In case you would not know much about static yet, it has been subject for a lot of controversy as beginners tend to think that the easiness of access is a good feature. Beware of that.
answered Sep 26, 2012 at 06:56 AM
Throw this utility script on any game object and it will not get destroyed when scenes change. Make sure that game object has your static data on it. Btw as far as I'm concerned, static data is faster to access than instanced data, so you're on the right track.
answered Sep 26, 2012 at 07:34 AM
Yes you can do this, but you need to specify everything as public.
Otherwise they default to private and your class will be useless.
Now you'll be able to use SceneData.width anywhere.
The other answers offer ways of having a class that inherits from MonoBehaviour which you can access from anywhere, but it doesn't look like you actually want any MonoBehaviour functionality, so a simple static class will work better.