- Home /
How to disable multiple renderer in gameobjects children
Hey, I need to toggle multiple Mesh Renderer which are all children of an game object per MouseDown.
Here you can see a picture of my scene:
All the houses on the tables should disappear if I click on one of them. I tried to find a solution for several hours now and came up with this script:
using UnityEngine;
using System.Collections;
public class clickonoffmulti : MonoBehaviour
{
void OnMouseDown()
{
gameObject.GetComponentInChildren<Renderer>().enabled = !gameObject.GetComponentInChildren<Renderer>().enabled;
}
}
For that, I put all the houses as children under a simple cube (as hitbox for clicking), but only the Mesh Renderer of the cube itself turns on and off. Is there a way to click on any of the houses and turn them all on and off at the same time? where should I place the script for that purpose?
Sorry for my noob question, I started with C# in Unity just some days ago;)
Answer by unity_cGCzaDBadOY4Ag · Feb 20, 2018 at 05:15 PM
As a temporary solution I put this code on each house and toggle them all on and off with GetKeyDown:
using UnityEngine;
using System.Collections;
public class clickonoffmulti : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.H))
gameObject.GetComponent<Renderer>().enabled = !gameObject.GetComponent<Renderer>().enabled;
}
}
Would still be nice to know how to toggle them all on and off with a mouse click on any of them;)
Your answer
Follow this Question
Related Questions
how do you toggle the visibility of child objects with a mouse over? 1 Answer
How to acess a Script in 2 spesific Children? 2 Answers
Parenting 1 object/Removing child/ pick up 1 Answer
Firing Rockets 1 Answer
SetActiveRecursivity Problem 1 Answer