Child object controll

Right now my script for raycast gets the parent of the object its pointing at. This causes a problem when I want to group objects together in a hierarchy for instance a set of drawers the individual drawers grouped with the parent if i click on the drawer all it sees is the parent. I could have the code look for a specific child object but I’m not exactly sure how to do that…I can search for children but that is not guaranteed to give me the child i was clicking on. Any suggestion or help would be greatly appreciated.

var raydistance : int = 50; // sets the distance the player has to be from an object in order to acces it
function Update () 
{


   if (Input.GetMouseButtonDown(0)) {      // If we click the mouse...
    //var ray = camera.ScreenPointToRay(Input.mousePosition);      // Gets the mouse position in the form of a ray.
	var hit : RaycastHit;
	var curserobject : GameObject;
		if (Physics.Raycast (Camera.main.transform.position, Camera.main.transform.forward, hit, raydistance)) 
		{      		 
			curserobject = hit.transform.root.gameObject;   //stores the selected object into curserobject			
			DoSomething(curserobject);  // Function call
		}	
   }   
}

not sure I followed, but why not:

curserobject = hit.transform.gameObject;

to get the game object you clicked on and not the root?

Thank you for the reply, yes that does get the object i clicked on but if its still a child in a hierarchy it still gets the parent well…at least that is what I think its doing in mine …

If you are actually talking about a set of drawers I could see issues arising if you have a colider on the chest itself.

If the colider covers the entire chest then it will envelop the drawers so when you click it hits that colider and not the drawers underneath.

A possible solution is to extend the drawer coliders ever so slightly beyond the reach of the chest’s colider. That way if the drawers are clicked on you get the specific drawer gameobject, otherwise its the chest.

Thank You for your reply. Well the problem here isn’t necessarily the colliders, That is taken care of in the import. And it will function properly if the drawers are not children of the Chester drawers (the drawers have no parent if that makes since, they are by themselves in the hierarchy) That works fine if you just need to activate that one object. However if the ground say the Chest + the drawers need to have an action preformed on them then you either have to make each drawer mirror the same action as the chest or you can group them in the hierarchy and run into my current problem.

What is happening here is when 3 drawers are opened at the same time the whole thing falls over via a hinge.