- Home /
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.
Answer by robertbu · Aug 03, 2014 at 11:16 PM
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[i].HitText(Input.mousePosition) {
Application.LoadLevel(i);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613155733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
an UP-TO-DATE menu tutorial 1 Answer
Drawing UI text on GUI.DrawTexture , How to do it ? 1 Answer
Game Menus WIth Keyboard Input Control 1 Answer
Moving GUIbutton over an object that does not follow the player? 2 Answers
Accessing the Asset's setting from code using Wizards/Editor classes? 0 Answers