- Home /
How do I simply hide an object in Unity?
Of all things, this has me puzzeled. How do I simply just hide an object?
It's not as simple as it sounds.. I need some way to hide an object that has dozens of root meshes inside it.
I have looked into this code:
// Disable the spring on all HingeJoints
// in this game object and all its child game objects
var hingeJoints : HingeJoint[];
hingeJoints = GetComponentsInChildren (HingeJoint);
for (var joint : HingeJoint in hingeJoints) {
joint.useSpring = false;
}
But, it's not really set up right.. I don't know.. I keep getting a "InvalidCastException: Cannot cast from source type to destination type." error
Answer by Lo0NuhtiK · Jan 27, 2012 at 05:35 AM
This isn't really "hiding" objects, so maybe re-word your question-title for search-purposes if you will.
I'm unsure of if this is the "right" way of doing things, but whenever I do stuff like you're trying and if I get those same errors, I just change the variable type to component and it's worked every time (so-far) ... Although, trying it with particles (I think it was particles anyway) did give me "warning" messages, but not errors.
Anyway, give that a shot wherever you're trying it -->
var hinges : Component[] ;
hinges = gameObject.GetComponentsInChildren(HingeJoint) ;
for(var joint : HingeJoint in hinges){
joint.useSpring = false ;
}
Well, it sort of is the goal..
So, now I'm trying to select a parent object by assigning the variable in the inspector.
var BodyVisual:Component[];
And then, this is how the code is:
BodyVisual=gameObject.GetComponentsInChildren(Renderer);
for(var joint:Renderer in BodyVisual){
joint.active = false ;
}
For some reason, this makes everything in my object with the script inactive. I want to select an object as a parent.. What should I do?
Ps: sorry, I don't know how to put the "code" format in when I'm commenting.
Ok.. so, you're wanting to disable the renderer "ONLY in the object this script is attached to" and NOT in the children?? Or what? I'm confused now. You wanted hingejoints, and are accessing children, but you really want a renderer and the parent?
In this line: var BodyVisual:Component[];
I want to select just the car body mesh (which has many roots) with the inspector and hide the car body mesh. If I disable everything inside the car that the script is attached to, then you are disabling some imortant things besides along with the car body, wich is not what I want. I hope that make more sense..
Why do you have them all stacked crazy like that? Is there a method in the madness? ... If not, you could make all those things "children" of Body_Visual, and hit'em all at once. I don't know how to go through 500 generations of objects in one shot, and don't feel like figuring it out at the moment either lol
To get them in one shot After making them children, not great^infinityGrandChildren objects , you could simply do this ->
var bodyVisual : Transform ; //drag it on here //then do whatever you're doing to call the renderer-disable stuff and for(var child : Transform in bodyVisual){ child.renderer.enabled = false ; //disable children renderers } bodyVisual.renderer.enabled = false ; //disable bodyVisual renderer
and be done with it.
EDIT :
NOTE : That image and the info that you're wanting to disable the renderers should have been what you originally posted in your question, not stuff about disabling springs in hingejoints and InvalidCast errors.Answer by Smorgon Magma · Apr 08, 2014 at 04:35 AM
It may sound too simple but just park the object off screen. Choose a position that's behind the camera and miles away. Make sure that you save the location before moving it then reload the position when you want the object back.
Your answer
Follow this Question
Related Questions
How do I mount a Vehicle? 4 Answers
enable/disable child objects? 2 Answers
Enable/Disable children / group of objects 0 Answers
Cs question from js user, how to get all children? 1 Answer
Disable any objects through 1 script 1 Answer