Hi,
Does anyone have a script to show or hide a prefab (or game object) on a key press?
The prefab contains multiple objects which should all be shown or hidden at once.
thanks
david
Hi,
Does anyone have a script to show or hide a prefab (or game object) on a key press?
The prefab contains multiple objects which should all be shown or hidden at once.
thanks
david
You can't show/hide a prefab as such, because a prefab is like a "template" of an object which could exist in your scene. You probably want to hide/show a gameobject (which might or might not be an instance of a prefab).
Do do this, you set the GameObject's renderer.enabled property to either true or false, like this:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// show
renderer.enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
renderer.enabled = false;
}
}
or, to toggle the visibility on and off with a single keypress:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// toggle visibility:
renderer.enabled = !renderer.enabled;
}
}
If you need to change the visibility of a GameObject and its children, you can loop through every object and change each one, like this:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
ToggleVisibility();
}
}
function ToggleVisibility() {
// toggles the visibility of this gameobject and all it's children
var renderers = gameObject.GetComponentsInChildren.<Renderer>();
for (var r : Renderer in renderers) {
r.enabled = !r.enabled;
}
}
(moved the toggling code to a separate function, which is often a good idea once any particular section of code starts growing significantly in size)
For Unity 5, you access game object’s rendered component with this syntax:
gameObject.GetComponent().enabled = false;
So the above answer would be:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
// show
// renderer.enabled = true;
gameObject.GetComponent<Renderer>().enabled = true;
}
if (Input.GetKeyDown(KeyCode.X)) {
// hide
// renderer.enabled = false;
gameObject.GetComponent<Renderer>().enabled = false;
}
}
Whit C# to disable Textures I use this code:
public void ChangeMenuActivity(bool state)
{
ToggleVisibility(gameObject.transform, state);
}
void ToggleVisibility(Transform obj, bool state)
{
for (int i = 0; i < obj.GetChildCount(); i++)
{
if (obj.GetChild(i).guiTexture != null)
obj.GetChild(i).guiTexture.enabled = state;
if (obj.GetChild(i).guiText != null)
obj.GetChild(i).guiText.enabled = state;
if (obj.GetChild(i).GetChildCount() > 0)
{
ToggleVisibility(obj.GetChild(i), state);
}
}
}
Tambien se puede hacer de la siguiente forma:
using System.Reflection;
void OcultarPadreHijos(object objGO)
{
PropertyInfo Propiedad= objGO.GetType().GetProperty("enabled");
Propiedad.SetValue(objGO,false,null);
}
Si quieres ocultar a sus componentes hijos, solo mandas a llamar a la función por cada hijo
Hey I know this is a pretty old thread, but I was hoping someone might have a bit of insight into a problem I’m having with this code (the last Toggle example) I am very new to Unity and am probably missing something obvious. I saved this as a script and childed it to the object (platforms) which I want to toggle:
But keep getting this error: Assets/Scripts/platforms_o_off.js(19,55): BCE0043: Unexpected token: …
#pragma strict
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.Z)) {
ToggleVisibility();
}
}
function ToggleVisibility() {
// toggles the visibility of this gameobject and all it's children
var renderers = gameObject.GetComponentsInChildren.(platforms);
for (var r : Renderer in renderers) {
r.enabled = !r.enabled;
}
}
Seems to be the only correct answer around, I will check it, but at least from reading the code what I guest It will work. Thanks a lot
This can be useful for someone.
Hide / Show UI objects for example Menu.
public void ChangeMenuActivity(bool state) {
ToggleVisibility(gameObject.transform, state);
}
private void ToggleVisibility(Transform obj, bool state) {
Image img = obj.GetComponent<Image>();
if (img != null)
img.enabled = state;
Text txt = obj.GetComponent<Text>();
if (txt != null)
txt.enabled = state;
foreach (Transform tr in obj)
ToggleVisibility(tr, state);
}
For Unity 5: You could also use the SetActive(bool) function
(name of Gameobject reference).SetActive(false);
for example:
car1.SetActive(false);
For CanvasRenderer you can use SetAlpha method, that worked for me fine.
hide:
gameObject.GetComponent().SetAlpha(0f);
show:
gameObject.GetComponent().SetAlpha(1f);
perhaps toggle visibility is the best way to go but I just turn off the object.
YourGameObject.SetActive (false); and then YourGameObject.SetActive (true);
doing it this way can lead to errors if you have a script still active referencing that gameobject, Like if a I enter a triggerzone that would normally effect that now inactive gameobject. But i simply add to that script " if (YourGameObject.activeInHierarchy == true)" . full example below
void OnTriggerEnter(Collider other) {
if(other.tag == "Player")
{
if (YourGameObject.activeInHierarchy == true)
{
animation.Play ("whatever");
}
}
You could just move it off screen or scale it. I find this works for complex gameobjects.
private Vector3 invisible = new Vector3(0.0f, 0.0f, 0.0f);
private Vector3 visible = new Vector3(0.48f, 0.48f, 0f);
private void Start(){
SelectedPanel = GameObject.Find("ToolTipUnit");
SelectedPanel.transform.localScale = invisible;
}