- Home /
How to toggle object visibility with keyboard button?
Hi, ive been searching for hours for a solution to this but everyone seems to say something different... and none have worked for me.
All i want to do is press a single button to toggle the visibility of an object. Not sure if it makes a difference, but the objects in question is under an empty gameobject parent (as ideally there would be multiple objects all being turned off via control of the parent).
Ive tried both with the parent and just targeting the object itself but obviously neither works. Ive tried so many different codes and variations that theres no point in posting them all here really. Id just like to know what particular methods work for you when essentially turning on and off an objects visibility. Full code please, im a noob and have no idea how to fill in the blanks :P
Thanks for any help/suggestions - they are desperately needed and greatly appreciated! :D
http://docs.unity3d.com/ScriptReference/Renderer-enabled.html http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$eyUp.html
If you have problems putting it together try some basics tuturials.
Show us what you got so far, also are you using C# or UnityScript(also called JavaScript even though it not JavaScript) Editet title, question speeks for itself
Yeah im using java/unityscript, and this was the closest ive come - but i feel like im completely on the wrong track:
function Start () {
}
function Update () {
if (Input.Get$$anonymous$$eyDown ("space")) {
ToggleVisibility();
}
}
function ToggleVisibility() { var renderers = gameObject.GetComponentsInChildren.();
for (var r : Renderer in renderers) {
r.enabled = !r.enabled;
}
}
it seems like it should be really straightforward? A few hours ago I thought it wouldn't be much more difficult than scripting the unchecking of the visibility boxes in the inspector hahaha...
Ive also tried this:
function Update () {
if (Input.Get$$anonymous$$eyDown ("space")) {
if (SnowScene.renderer.enabled = true) {
SnowScene.renderer.enabled = false;
}
}
else {
SnowScene.renderer.enabled = true;
}
}
where "SnowScene" is the empty gameobject parenting the actual objects. What am i missing?
Answer by Linus · Nov 08, 2014 at 07:21 AM
This should do it
#pragma strict
var isVisible : boolean = true; //Start as on.
function Start () {
}
function Update () {
if (Input.GetKeyDown("space")) {
if(isVisible == true){
ToggleVisibility(false); //Tell toggle inside ToggleVisibility to be false
isVisible = false;
} else {
ToggleVisibility(true);
isVisible = true;
}
}
}
function ToggleVisibility(toggle : boolean) {
var renderers : Renderer[] = gameObject.GetComponentsInChildren.<Renderer>();
for (var r : Renderer in renderers) {
if(toggle == true){
r.enabled = true;
} else {
//Toggle is not true so must be false
r.enabled = false;
}
}
}
I guess you also could do:
if(r.enabled){
r.enabled = false;
} else {
r.enabled = true;
}
Ins$$anonymous$$d of having the toggle and isVisible variables
OH $$anonymous$$Y GOD! Thankyou you are a LEGEND!!! Looks like my mixed media final assessment is going to be handed in after all!! :D cannot thank you enough, that did it perfectly! :D