In practice, loading a Scene during runtime using either the SceneManager.LoadScene or SceneManager.LoadSceneAsync methods, we see that both methods take either an integer for the build index or a string for the scene name. This means that for our MonoBehaviours that want to load a specific scene that is configured via the Inspector, we have to make a public int, or as i used to prefer, string.

This is of course suboptimal, so instead of keeping the complain-train going, let’s fix that with our very own custom attribute!

ATTRIBUTES SAVE THE DAY

Since we’re forced to use a string for the scene name, there is ample room for messing up by misspelling a scene name in the Inspector window. That is of course no good, as the we’ll only notice that something has gone wrong when we play the game and get an error! Instead we would like the string that we input to be validated automatically, ensuring that a scene exists with the same name in the build settings. The sexiest way to do this is by making a PropertyAttribute that we can attach to our serialized string field. Have you ever put [Range(0f, 1f)] over a float or an int to limit the values that can be set in the Inspector, while making Unity draw the field as a nice slider? That’s a PropertyAttribute, and we’re gonna make our own now!