-1

I have a model of human body which consists of hundreds of sub-models (muscles).

I want to click on the sub-model and get its name.

Basically, I know two possible solutions.

  1. To create stupid script OnButtonDown for every single sub-model.. which is insane.
  2. Create script using raycasting

I've tried the second one, didn't work for my sub-models.

As for the first one, I was thinking whether there is some kind of inheritance.. that I could move one script for Model and it would be applied on all its sub-models. I was trying to find the way to do that, but I have failed.

Any Ideas?

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
Michal Krča
  • 149
  • 3
  • 15

1 Answers1

1

Make sure all of your sub objects have colliders on them, and then create a javascript (unityscript) asset with the following code:

function Update () 
{
    if (Input.GetButtonDown ("Fire1")) 
    {
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast(ray, hit)) 
        {
            Debug.Log(hit.collider.gameObject.name);
            Destroy(hit.collider.gameObject);
        }
    }
}

Attach this to an object that is always alive (like the main camera).

JohnD
  • 1,129
  • 10
  • 19
  • Thank you, but I have another problem here. Now I have selected the game object, but even though I am trying to change its material (color), it does not work. Because the texture is mapped on several sub models at once. – Michal Krča Feb 04 '14 at 10:21
  • Probably best to ask this as a separate question, with a little more detail. – JohnD Feb 04 '14 at 12:09