|
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
(comments are locked)
|
|
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:
or, to toggle the visibility on and off with a single keypress:
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:
(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) Thanks for an extremely quick reply! those scripts work well for single meshes :) however my prefabs have multiple meshes - how could i apply it to an entire instance of a prefab? [let me know if the proper etiquette on this site is to post a new question or not]
Mar 31 '10 at 03:29 PM
David 3
if the question is just a small addendum to the original question (like yours is), a comment is best. If it's more like "the next question" in your larger set of goals, a new question is best.
Mar 31 '10 at 03:42 PM
duck ♦♦
edited to include gameobjects with children (I'm guessing this is what you meant by 'multiple meshes', right?)
Mar 31 '10 at 03:47 PM
duck ♦♦
doh! accidentally dropped a bit of C# in there! Yes, changing 'bool' to 'var' will fix that. But also, the script assumed that the 'parent' gameobject had a renderer, wheras I'm guessing your's doesn't (only the children have them?). I have fixed that now too (which completely removes the need for that var anyway!) Duck 37 secs ago
Mar 31 '10 at 04:28 PM
duck ♦♦
ok - yeh I use c# for work, but tend to go with JS when helping out (if no preference is specified) because most c# people can convert from JS, but not the other way around! ;)
Mar 31 '10 at 04:29 PM
duck ♦♦
(comments are locked)
|
|
Whit C# to disable Textures I use this code: You really should instead adapt Ben's code into C# - it should perform much better. And, since I'm already criticizing your code, that's setting, not toggling visibility. Good naming can make a lot of difference.
Jun 25 '12 at 04:34 PM
Cawas
(comments are locked)
|
|
Tambien se puede hacer de la siguiente forma: using System.Reflection; void OcultarPadreHijos(object objGO) { } Si quieres ocultar a sus componentes hijos, solo mandas a llamar a la función por cada hijo
(comments are locked)
|

if you mean to completely disable it: http://answers.unity3d.com/questions/35363/gameobjectactive-not-deactivating-all-children.html