Turning off Childrens' Renderers
I'm currently working on a script that turns off a character when he dies so that you can't see him and he cant' move. I've been following a Tutorial and the code that he gave works perfectly, but the script only turns off the Parent object's renderer. I need it to turn off it's children. Here's the code that I have.
using UnityEngine; using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private Movement player;
private Renderer rder;
public GameObject deathParticle;
public GameObject respawnParticle;
public float respawnDelay;
void Start ()
{
player = FindObjectOfType<Movement> ();
}
void Update () {
}
public void RespawnPlayer()
{
StartCoroutine ("RespawnPlayerCo");
}
public IEnumerator RespawnPlayerCo()
{
Instantiate (deathParticle, player.transform.position, player.transform.rotation);
Debug.Log ("Player Respawn");
player.enabled = false;
player.GetComponent<Renderer> ().enabled = false;
yield return new WaitForSeconds (respawnDelay);
player.transform.position = currentCheckpoint.transform.position;
player.enabled = true;
player.GetComponent<Renderer> ().enabled = true;
Instantiate (respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
}
All of this works perfectly, but I need it to turn off the children of the Player as well as the player.
PS I'm fairly new to coding so go easy if I don't understand your explanations xP
Answer by ZiadJ · Aug 23, 2015 at 09:45 PM
You can use GetComponentsInChildren() for this. This method should do the trick:
public static void EnableRendererRecursively(GameObject player, bool enable)
{
var renderers = player.GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
renderer.enabled = enable;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612001016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Parsing Error CS8025 on Last Line? 2 Answers
Unity crash, now errors and deleted objects reappeared.. 0 Answers
Spawn and drag on spawn multiple objects with android multiouch 0 Answers
adjust image size with mouse 0 Answers
SQL Unity Android 0 Answers