How do i script so that i can load a scene by typing its name in the inspector? The script is for a “play” button to load a specific scene.
Do i use SerializeField in the script?
Something like this? but not as a value as i have here.
using UnityEngine;
using System.Collections;
public class ButtonClick : MonoBehaviour {
[SerializeField]
private int LoadLevel;
void OnClick()
{
Application.LoadLevel(LoadLevel);
}
}
If you want to load it by name, make the ‘LoadLevel’ a string instead.
SerailizeField is really only needed if the field is ‘private’, as usually unity doesn’t serialize private fields (though really… I’ve seen it still serialized private fields, just not show them in the inspector… grrr ugh, annoying as crap).
Do note, you’ll have to go to the build settings and include what scenes you want loadable into the build settings there.
All good. I generally recommend using the string form instead of the int form. The int form can get a bit gnarly if you add scenes in the middle or start of the build settings.
The only time I use the int form is when I’m loading levels in a relative fashion. As in reload this level, or load the level after this one.