0
\$\begingroup\$

I want to access specific colliders(to see if they are been clicked) from one script insted of making multiple scripts for every collider.How?

I am using unity.

using UnityEngine;
using System.Collections;

public class MainMenuManager3D : MonoBehaviour
{
    public enum Menu
    {
        MainMenu,
        NewGameMenu,
        LoadGameMenu,
        OptionsMenu,
        GameSettingsMenu,
        GameGraphicsMenu,
        Exit
    }
    public Menu currentMenu;
}

I want to change the currentMenu based on which collider is clicked in this script. Insted of making 99999 scripts for every collider.

\$\endgroup\$
4
  • \$\begingroup\$ Which engine are you using? Can you be a little more specific with your question (maybe post some example code)? \$\endgroup\$ Jan 21, 2015 at 23:24
  • \$\begingroup\$ I edit it including the code. \$\endgroup\$
    – KALALEX
    Jan 21, 2015 at 23:28
  • \$\begingroup\$ Are you using the new Unity GUI system for this? \$\endgroup\$
    – bummzack
    Jan 28, 2015 at 10:08
  • \$\begingroup\$ @bummzack No, i don't.I am scripting it becuse i want an interatctive menu based on where the player is standing.But also i want the 3D environment to be "in sight" and block visual contact with the menu. \$\endgroup\$
    – KALALEX
    Jan 28, 2015 at 19:15

2 Answers 2

1
\$\begingroup\$

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.

enter image description here

Thats it! You have visually appealing, fully functional buttons!

\$\endgroup\$
1
  • \$\begingroup\$ Thanx man you have covered my question 100000%. \$\endgroup\$
    – KALALEX
    Jan 23, 2015 at 16:44
0
\$\begingroup\$
OnCollisionEnter(Collision collision){
if(collision.gameObject.name == foo){
//do foo
}
else if(collision.gameObject.name == bar){
//do bar
}
}
\$\endgroup\$
5
  • \$\begingroup\$ Please add some explanations to your answer. A bunch of code thrown around is confusing. \$\endgroup\$
    – user15805
    Jan 22, 2015 at 15:22
  • \$\begingroup\$ Yes but i want to accesses an other object, not this objects' collider. \$\endgroup\$
    – KALALEX
    Jan 22, 2015 at 16:39
  • \$\begingroup\$ @KALALEX the name of the collision is the name of the thing colliding with the gameObject that the script is attached to \$\endgroup\$
    – Evorlor
    Jan 22, 2015 at 17:04
  • \$\begingroup\$ No i don't want to check the object that the script is attached to! I want to check multiple objects at the same time if they are been clicked by the mouse and if so do something. In ONE script. \$\endgroup\$
    – KALALEX
    Jan 22, 2015 at 17:30
  • \$\begingroup\$ @KALALEX im sorry. I'm not really understanding the question \$\endgroup\$
    – Evorlor
    Jan 22, 2015 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .