x


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

more ▼

asked Jan 27 '12 at 04:46 AM

Rush3fan gravatar image

Rush3fan
293 32 43 47

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 ;
}
more ▼

answered Jan 27 '12 at 05:35 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 9 39

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.

Jan 27 '12 at 05:54 AM Rush3fan

Also, this has nothing to do with joints of course..

Jan 27 '12 at 05:56 AM Rush3fan

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?

Jan 27 '12 at 06:06 AM Lo0NuhtiK

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..

Jan 27 '12 at 06:17 AM Rush3fan

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.
Jan 27 '12 at 07:39 AM Lo0NuhtiK
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x216
x184
x58

asked: Jan 27 '12 at 04:46 AM

Seen: 2232 times

Last Updated: Jan 27 '12 at 10:40 AM