One script for multiple GUIObjects (i.e. lvelselector)

I want to make a leverselector and made a scene with an empty gameObject called “Levels”, then I added a couple of GUITexts in the empty gameObject like:

1 2 3 4 5

6 7 8 9 10

etc…

and I was wondering if there is any way of adding a script to the empty “Levels” object and do like a switchcase, kind of like:

function OnMouseDown ()
{
    if (guiText.collider2D == "1")
    {
        //load level 1
    }
}

instead of adding a script to each and every single one of the guiText objects.

You cannot do it the way you are suggestion. OnMouseDown() must be on the game object tested. What you can do is to first put your GUIText objects in an array in a script on the Levels object:

var levelTexts : GUIText[];

Next select the Levels game object in the hierarchy, open up and set the size of the ‘levelsTexts’ array to the number of levels, then drag the GUITexts for each level into the array in order.

Then you can do something like this in a script on your Levels game object:

function Update() {
    if (Input.GetMouseButtonDown(0)) {
         for (i = 0; i < levelsTexts.Length; i++) {
             if (levelsTexts*.HitText(Input.mousePosition) {*

Application.LoadLevel(i);
}
}
}
}