- Home /
Global Scripts/Variables
Hi, I'm creating a game with multiple levels. I'm trying to grab the native Resolution of the screen my game is running on and store it. Then use this native resolution value and divide by the current resolution to get a ratio. (This way I can scale things, mainly my GUI for different resolutions)
(Feel free to suggest other ways to do this but ultimately I would like to learn how/if it's possible to have global scripts that work on every level)
Thanks in advance
EDIT :
The way I'm currently doing this is creating an Empty game object and attaching a script, on Start() I use DontDestroyOnLoad(gameObject); However this causes some issues with null values in my script :(
Obviously, you have the option of just using static classes for this, but from a software design perspective that's pretty stinky.
I think it's stinky because it breaks the rules of object oriented program$$anonymous$$g. There's pretty widespread discussion on this subject elsewhere, but this page pretty well sums up my feelings on the matter.
Well, initially I would have suggested that 'empty gameobject/DontDestroyOnLoad()' thing, but you say that causes problems for you. Care to elaborate? It's what I've always done, and it's never given me any trouble.
On an unrelated note, are you sure resRatio should be an int? That looks wrong, unless it does something different from what I think it does.
As for the invalid values thing, I can think of 3 things that might be happening:
1: the object is being serialised and deserialised, like what happens when you reload your assemblies at runtime. I can't really tell why that would be happening, but the symptoms are the same.
2: there is a version of the object in the newly loaded scene that is confusing other parts of the system? Now that I think about it this seems unlikely, as it's a pretty obvious mistake to make, but still worth checking. How are you accessing this object elsewhere?
3: Start is being called more than once. Again, this is something that shouldn't happen.
@Hoeloe 'executes faster' and 'good practice' are not always the same thing. Unless performance is your number 1 priority it is generally better to adhere to OOP principles where possible. That link of yours explains that static functions are faster (which, for that matter, is fairly implementation-specific and may not even apply to C#), not that they are better.
Answer by Bampf · Sep 18, 2013 at 11:41 AM
Yes, it is possible to have global scripts that work on every level.
You can add static methods to any class that you define. These methods don't need an instance of the object to be called. You would call something like "MyClass.getScreenRatio" instead of someObject.getScreenRatio.
The trick there is that you want it to work regardless of which scene is loaded first. But this isn't hard, you just need to initialize it the first time it is used if it hasn't been initialized yet.
private static Vector2 screenRatio = null;
public static Vector2 getScreenRatio()
{
if (screenRatio == null) {
screenRatio = // do calculation here
}
return screenRatio;
}
As for your original trick (empty game object, DontDestroyOnLoad) that can be made to work. You were getting null errors but I suspect those could be solved with a similar trick to the one I showed above, to guarantee that something is initialized the first time it is accessed.
This game object approach has a very compelling feature, which is that the object will get all the Unity calls (OnUpdate, etc.) However there is a major problem with that strategy, which is that it requires you always have to run your game starting from the level that defines the empty game object. You couldn't debug a different scene in the Unity editor, for instance. So then you end up adding the same empty game object to multiple levels, but then every time you switch scenes you get another one.
What you'd want is a game object that always is around, but there is always exactly one of them, no more, no less. This problem is called a Singleton. Here's a good discussion of ways to do them in Unity: http://redframe-game.com/blog/global-managers-with-generic-singletons/
@Bampf - if the code you posted is supposed to be a method, add parentheses. If it is property, add a getter.
Thanks. I had intended it as an outline/pseudocode (note it's still incomplete, the commented line won't compile). But I agree with you that it was misleading and have added them.
Your answer
Follow this Question
Related Questions
Control Permition Access 1 Answer
Global array list c# 1 Answer
Global variables questions 5 Answers
Need To Incorporate App Installation Access To `Identity`. 0 Answers
how to read a Microsoft access database in unity 3d 0 Answers