- Home /
Disable an child object
function CheckDeath()
{
if(state!=_State.Dead)
{
if(Hp<=0)
{
disabled=true;
state=_State.Dead;
var CtP=GameObject.Find("ContactPoint");//name all weapons or dmg points this for monsters.
CtP.collider.enabled=false;
var chance=Random.Range(1,100);
if(chance>49)
{
Instantiate(PowerUpDrop,transform.TransformPoint(0,Random.Range(1,3),Random.Range(-1,-3)),Quaternion.identity);
}
targetinfo.LevelExp+=ExpGiven;
animation.Play("ko");
yield WaitForSeconds(5.0);
disabled=false;
Destroy(this.gameObject);
}
}
}
I'd like to disable the contact point object (var CtP). However using the code I have now, it disables this object on all objects in the scene. Is there anyway I can make it so that the contact point for only one gameobject gets disabled?
You need better search criteria for finding CtP. Is it a specific object that you want to disable? Try using
GetComponentsInChildren(Collider);
to get an array of colliders in the hierarchy below your current transform. Then use a for loop to disable all of them. Is there one, or more of these on each object? If so, think about setting it up in the inspector, and bypassing this whole 'GameObject.Find()' thing. (I personally never use GameObject.Find(string)- there is always a better way!)
Answer by PatrikNyblad_SI · Oct 14, 2011 at 07:50 AM
Hi!
You can look at this post that I recently answered to. It shows how to access different objects in different hierarchies and also how to access a component in a good way.
http://answers.unity3d.com/questions/176498/a-quick-way-to-find-the-root-parent.html#comment-176538
I'm not trying to disable the parent. I'm trying to disable the child so how does this help me?