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.

gman
  • 100,619
  • 31
  • 269
  • 393
Melyy3
  • 29
  • 4

1 Answers1

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;
});
GuyGood
  • 1,377
  • 1
  • 9
  • 12
  • Thank you for the reply. I also tried that but I just get the error: Uncaught TypeError: Cannot read property 'traverse' of undefined. – Melyy3 May 07 '14 at 15:42
  • 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 – GuyGood May 07 '14 at 17:13
  • 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. – Melyy3 May 07 '14 at 17:28
  • 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? – Melyy3 May 07 '14 at 18:06