- Home /
Make a suboject visible using C#
I am trying to modify a C# script so that a subobject becomes visible upon a collision event. Below is how the object looks in the hierarchy:
-EnemyEasy (contains collider, rigid body, C# script)
Enemy (Contains various subobjects, I want this visible all of the time)
EnemyWithCoins (Contains various subobjects, I want this to become visible when the Root object's collider comes in contact with another collider)
Any help is greatly appreciated.
Answer by robertbu · Feb 15, 2014 at 07:56 PM
I not completely sure of how you have things setup, but here is a script that when the parent receives a collision, the children are made visible:
#pragma strict
function OnCollisionEnter() {
var renderers = transform.GetComponentsInChildren(Renderer);
for (var rend : Renderer in renderers) {
rend.enabled = true;
}
}
Also note you may want to remove or disable the colliders on the sub-objects depending on your use. Even though the renderers are disabled, the colliders will still be active unless you remove or disable them. Also note that 'GetComponentsInChildren()' is a bit misnamed since it also returns the parent you used in calling the function.
Your answer