Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Crewman In Red · Jan 21, 2011 at 04:04 PM · objectvisibility

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jan 27, 2011 at 05:11 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.&lt;Renderer&gt;();
 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; }

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Crewman In Red · Jan 21, 2011 at 04:57 PM 0
Share

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.

avatar image Jessy · Jan 21, 2011 at 05:03 PM 0
Share

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.

avatar image Jessy · Jan 27, 2011 at 06:39 PM 0
Share

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

avatar image Crewman In Red · Jan 27, 2011 at 08:00 PM 0
Share

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.

avatar image Crewman In Red · Jan 27, 2011 at 08:00 PM 0
Share

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?

Show more comments
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges