The correct design choice here would be making one script and attaching it to the menu elements (I assume buttons) and define what each button will do on OnCollisionEnter()
or OnMouseButtonDown(0)
. The main idea here is to differentiate clicks based on the name of the object that is being clicked on.
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "NewGameMenu")
{ // or whatever the name of the button object is
// start new game
}
if(col.gameObject.name == "Exit")
{
Application.Quit();
}
}
Or in the mouse button case (if your design is built on that type), you can ray cast from the mouse click:
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
RayCast();
}
}
void RayCast()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.gameObject.name == "NewGameMenu")
// do stuff
.
.
.
}
}
That should cover your question, I think,
HOWEVER,
I strongly recommend you use the new UI system on Unity 4.6.
Use new UI features (Canvas, Button, Text) and assign functions to button clicks. The button animations are already there, which is quite cool! In case you need tutorials, check Unity's tutorials on the new UI System!
Make a menu manager object and define the functions that you want them to be called upon a button press. Simply like this:
public void MainMenu()
{
Application.LoadLevel("menu");
}
public void Quit()
{
Application.Quit();
}
public void Play()
{
Application.LoadLevel("game");
}
Finally, go and assign the functions to the buttons.
Thats it! You have visually appealing, fully functional buttons!