Using instantiate i had created obj1, obj2, obj3…
All these objects have child with similar names roof, wall etc.
if the character collide with a specific object,
eg : obj1
i just want to disable only roof(a child) of obj1
and not complete obj1 without using below code
var renderers = GetComponentsInChildren(Renderer);
for (var r : Renderer in renderers) {
r.enabled = false;
}
for loop takes more time.
any other easy way to disable .
Thanks in advance.
I’m not really sure of what you want but just to be sure I gonna give you more solutions :
First you say when the player player hit one of this object, I want to disable this object but in your example you say the roof which is a child of the object collide.
So do you want disable the global object or just the roof if the player collide with the roof ?
Then you say disable but enabled just hide the renderer of the object so you just wanna hide it or totally disable it ?
If in your object each child have his own collider you can do this :
function OnCollisionEnter(collision : Collision)
{
collision.gameObject.active = false;
//Or if it's just graphic you can do this
collision.gameObject.renderer.enabled = false
}
If you want disable all objects of the collided object you can do this :
function OnCollisionEnter(collision : Collision)
{
//This don't just hide them but disable them and it's obviously a function that hide a loop
collision.gameObject.SetActiveRecursively( false );
}
But without a loop I don’t think you can disable the childs of an object without a loop to iterate on them