0

I want to hide/reveal objects with the click of a button. i.e. click button on a GUI and obj1 is hidden and obj2 is revealed. I was told to do this using

 object.traverse(object.visible=false);

but it does not seem to work.

Here is how I'm rendering my objects

var gal = jsonLoader.load( "model/galmodel.js", addModelToScene ) ;
gal.traverse(gal.visible = false);

Can anyone point me into the right direction on how to make this work? And the command that will hide/reveal the objects on click?

Thank you very much.

1 Answer 1

1

Last time i checked, the traverse function was a callback. So you would need to do something like this:

gal.traverse(function(child){
   child.visible = false;
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reply. I also tried that but I just get the error: Uncaught TypeError: Cannot read property 'traverse' of undefined.
Well, you will have to make sure your "gal" variable is set of course. You should check the loader examples on the three.js homepage how loading is done properly. As I recall, the load-Function is callback-based, too so you will maybe have to set "gal" inside your callback function addModeltoScene? Not sure what the jsonloader.load function itself returns
Thanks again for the reply. My variable "gal" is defined as : var gal = jsonLoader.load( "model/galmodel.js", addModelToScene ) ; It is also set inside the addModeltoScene as: function addModelToScene(gal) {} etc. The object renders properly, I just want to be able to show/hide it through whatever function permits it. The traverse function continues telling me that it is undefined. Thank you again.
Figured it out. Just simply had to add that function inside the addModeltoScene function for that object. Thank you very much. Now I just need to be able to hide this object and show another with the push of a button. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.