Search code examples
c#staticunity-game-engineaspect-ratiogameobject

Unity static variable on every screen


I'd like to create a class which decides the aspect ratio (like 16:9, 4:3, so the camera.aspect property is not enough for me), when the app starts, than I want to reach this class on every scene. So I don't want to attach my script to any gameobject, because when the scene disappears, and another scene appears, I'll not have that gameobject anymore, I want this value to be reached everywhere, and count only once. How can I achieve that?


Solution

  • Create a static class. Static classes will stay alive during the whole execution of the program.

    Then store the information you want in variables. Then use setters and getters to grab the information.

     public static class Test
     {
         private static int x;
    
    
         public static int foo()
         {
             return x;
         }
     }