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 Exalia · Sep 18, 2013 at 10:35 AM · androidaccessglobalglobal variableglobal script

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 :(

Comment
Add comment · Show 15
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 syclamoth · Sep 18, 2013 at 10:54 AM 1
Share

Obviously, you have the option of just using static classes for this, but from a software design perspective that's pretty stinky.

avatar image syclamoth · Sep 18, 2013 at 11:06 AM 1
Share

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.

avatar image syclamoth · Sep 18, 2013 at 11:11 AM 1
Share

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.

avatar image syclamoth · Sep 18, 2013 at 11:35 AM 1
Share

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.

avatar image syclamoth · Sep 18, 2013 at 11:41 AM 1
Share

@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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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/

Comment
Add comment · Show 2 · 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 ArkaneX · Sep 18, 2013 at 11:53 AM 0
Share

@Bampf - if the code you posted is supposed to be a method, add parentheses. If it is property, add a getter.

avatar image Bampf · Sep 20, 2013 at 06:19 PM 0
Share

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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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