Hello,
Like my title says, How do I make an object appear or disappear when pressing an arrow key?
Hello,
Like my title says, How do I make an object appear or disappear when pressing an arrow key?
Methods for making an object appear or disappear include (but are not necessarily limited to):
Which method to use depends on the context and on what effect you want.
Okay. The third one might seem best. How would I do it?
Set the ‘active’ property of the game object in question to ‘false’.
(Note that this will effectively ‘hide’ the game object from search functions such as GameObject.Find(), so if you’ll need to re-activate it, you’ll want to save a reference to it. Note also that simply disabling the renderer doesn’t have the same problem.)
Hmmm. How can I enable/disable the renderer?
renderer.enabled = true or renderer.enabled = false.
Thanks. It works.
Hi Jesse, If i use the renderer.enable =false then the object is still active right and will take processing power for other things like physics calculation and stuff right?
Yes, it is still active, and I’d imagine that most if not all other processing proceeds as normal even if the renderer is disabled. (That is, update functions will be called, physics will be simulated, etc.)
Disabling the renderer should really only affect the object’s visual representation; everything else should continue to function as normal.
Then i think the option of activating/deactivating is much better then using renderer.enable. Because it will avoid problems like unintentional script running attached to the object or physics interfering with other objects. renderer.enable would be better if we want to make something invisible but with the properties still affecting the world.
Or, you could just attach it to the object or not add a rigidbody to it.
Disabling the renderer makes the object invisible, assuming there is only one renderer in the transform tree. Disabling the game object turns off everything, including animations, scripts etc. However if your show/hide script is somewhere in the same transform hierarchy, that script will of-course also no longer be running and will therefore not be able to re-enable the game object.
Disabling the renderer directly answers the asked question, but I suppose the question is whether or not that is actually what OP wanted.
id say the best method would depend on the situation. if the object has no extra scripts or wouldnt cause problems if accidentally walked over or collided into then just hiding the renderer would be fine, but if the user can by mistake walk into it triggering something then id say to place it as a variable in the “hide” script, obviously making sure that script isnt on the object that will be deactivated attach it to the player or camera or somethin