How to change this script to be reused

This script allows me to set a key to load a specific level. However to do this this requires creating a new script every time and I need to make it reusable. I have seen other scripts being reused by adding variables but I don’t know how or if you can do that with this one:

function Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        Application.LoadLevel(2);
    }
}

Any suggestions?

You need to externalize the KeyCode used as input. To do so create a class variable outside the Update function, so that it can be changed in the Inspector.

var loadLevelKey : KeyCode = KeyCode.Space; // KeyCode.Space will be the default value

function Update()
{
    if (Input.GetKey(loadLevelKey))
    {
        Application.LoadLevel(2);
    }
}

You can do the same with the level id:

var loadLevelKey : KeyCode = KeyCode.Space; // KeyCode.Space will be the default value
var levelID : int = 2; // by default level 2 will be loaded (keep in mind that level id starts at 0).

function Update()
{
    if (Input.GetKey(loadLevelKey))
    {
        Application.LoadLevel(levelID );
    }
}

var level : int;

function Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        Application.LoadLevel(int);
    }
}

just change the int value in the inspector and u r set