- Home /
Toggling visibility of two gameobjects.
I want to swap two hierarchies (with lots of children each) on a keystroke. I found this script on another question which works great for switching the visibility of said hierarchy.
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;
}
}
My question is how do I modify this script to set the initial visibility of my second hierarchy (and all it's children) to false? I believe I would have this script on the initially visible hierarchy and another script with initial visibility off on the second hierarchy. Then the keystroke would properly switch the two. I don't know how to implement this in the script. Thanks for any help you can provide.
Editted for proper terms (thanks for setting me straight Jessy)
Edit: 5 days and still no help?
Sorry, answerers don't get notified when you edit the question. It is probably best to add a comment when you do so. I'm not sure if the comment needs to be attached to the answer, or if you can comment on the question itself, but I know at least the former works. Updated answer co$$anonymous$$g up shortly.
Answer by Jessy · Jan 21, 2011 at 04:45 PM
You haven't mentioned if it's acceptable to just store the enabled state of all the renderers in the Editor, and then switch them all during gameplay. I'm going to go with that assumption for this code example. Please comment if that is not what you need.
import System.Collections.Generic;
pragma strict
@ HideInInspector @ SerializeField private var renderers : Renderer[];
if UNITY_EDITOR
ar parents : List.<GameObject>;
function Reset () { if (parents == null) return;
var renderers = new List.<Renderer>();
parents.ForEach(function(parent) {
for (var rendererComponent in parent.GetComponentsInChildren(Renderer))
renderers.Add(rendererComponent as Renderer);
});
this.renderers = renderers.ToArray();
} #endif
function Update() { if (Input.GetKeyDown(KeyCode.Z)) for (var i = 0; i < renderers.Length; ++i) renderers[i].enabled = !renderers[i].enabled; }
Just populate the parents List in the Inspector, and choose Reset from the script's drop-down menu after you have all the renderers set up the way you like.
I don't like the way Generics look in UnityScript, and the Generic form of GetComponents seems to be broken. Here's what the ForEach statement can look like, in C#, and ideally what it could look like in UnityScript.
parents.ForEach(parent => renderers.AddRange(parent.GetComponentsInChildren<Renderer>()) );
Here's an Editor script. Select any game object with a renderer, and then pick this menu item from the GameObject menu, and all the renderers in its parent's hierarchy will be set to whatever it is not, enabled-wise. You can, of course, just select the parent itself.
@ MenuItem ("GameObject/Toggle Renderers Recursively", false, 1011) static function Toggle () { var switchEnabled = !Selection.activeTransform.renderer.enabled; for (var rendererComponent in Selection.activeTransform.root.GetComponentsInChildren(Renderer)) (rendererComponent as Renderer).enabled = switchEnabled; }
@ MenuItem ("GameObject/Toggle Renderers Recursively", true) static function ValidateToggle () : boolean { return Selection.activeTransform && Selection.activeTransform.renderer; }
Not sure what you mean about the hierarchy... $$anonymous$$y game objects do have children, lots of them. That's why this script works for me. I just can't seem to set the second objects visibility initially to false.
You didn't mention that, at all, in the question. You only talk about two objects, not two hierarchies, or one hierarchy and another single object. Please clarify exactly what you need.
Edited. fgsfgaarfnawrkljklasdjklfgsjgsrargfaregarel;asfjarekrejkI HATE YOU STUPID UNITYANSWERS ALL I WANT TO SAY IS "EDITED" YOU PIECE OF GARBAGE 8 $$anonymous$$ORE CHARACTERS ARE JUST USELESS ^&#^&%$@&^^U@^&^%&@ :-D
Thanks again for your help Jessy, I really appreciate it since I don't know much about scripting. I applied the script from above and used it to assign the two parent objects. When I press the z key in the game the first parent in the list will then be hidden but the second isn't effected.
I know I haven't explained my purpose too well and I'm sorry for that. This is how we learn I guess. What I was thinking, and if you know a better method than please offer it, is that I have two building parents with lots of children each. On a keystroke I'd like to switch from the first option (parent 1) to the second (parent 2).That way the client can see one option at a time on the exact same site. Does this make better sense?
Answer by vished · Feb 16, 2011 at 09:41 AM
That looks like a ridiculous amount of code to hide a parent and all of it's children - surely there's an easier way? Can someone post a simple example with key press that hides a hierarchy? My geo is visible but it has no rendering component? Why isn't there a hierarchical visibility attribute like every other 3D software? Why do I have to search every object in the game using a "find"? I should be able to refer to an object in a hierarchy with some kind of simple syntax but I can't see any explanation in the docs!
Your answer
Follow this Question
Related Questions
Is particular object visible 1 Answer
Object Visibility 2 Answers
Objects Appearing 1 Answer
turnoff visibility 1 Answer
GUI On button click change visibility of array objects 0 Answers