- Home /
Is there a way to make a script do something every time a new level loads?
I have a bit of a problem, I've just recently added graphics options to my game, and it works fine, that is until I load a new level, then everything, with the exception of texture quality, resets.
I've made it so that the object that stores the graphic options data, doesn't get destroyed when a new level loads (DontDestroyOnLoad), but that doesn't seem to do much, I need to somehow make it so that when a new level is loaded, it sets all of the graphic options to the users preference, is there anyway I can do that?
I think I remember hearing about a void that does that, but I can't for the life of me remember what it was called, and I can't find the video where I heard about it.
Also, I'm wondering if it's possible to detect the aspect ratio of the players resolution, because I have loading screens set up for the game, and want to have widescreen load screens for widescreen resolutions and a normal ratio for the others.
Why not just call some code in the Awake() function?
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Awake.html
It gets called every time the script is initialized i.e. whenever a new scene is loaded.
You could create a script which sets the graphics to the user preferences in the Awake() function and then add the script to each scene you want to load the graphics options for.
It doesn't seem to work, adding the apply function to the "Awake()" function doesn't seem to do anything.
@merry -- he means (I assume) when you are using DONTDESTROYONLOAD
in that case Awake() DOES NOT run each time there's a level change. jarz has given dah answer.
Answer by janzdott · Apr 10, 2013 at 03:10 PM
Monobehaviour.OnLevelWasLoaded (int level) will fire when a certain level is loaded. If I were you, I would have the graphics object get destroyed when changing scenes, then just have one graphics object in every scene that you need it. Then whatever code you put in Awake () or Start () will get executed at the start of each scene. And Camera.aspect will return a float that is the aspect ratio (width/height)
The problem with that is that the graphics object gets it's values from the menu objects in the menu, it doesn't reference them, but the objects themselves set the values through a static void, Do the object is first started it's blank. Which means the graphics won't apply to every new level, because the settings are back to default.
The solution you posted worked anyway, thank you!
One thing you could do, to keep the settings between scenes, is use static variables for the fields you wish to keep. That's the easiest solution. Or you could use playerprefs or serialize them to a binary or xml file and load that file when changing scenes
Your answer
![](https://koobas.hobune.stream/wayback/20220613100042im_/https://answers.unity.com/themes/thub/images/avi.jpg)