Search Unity

Toggling visibility of geometry with a keyboard press

Discussion in 'Scripting' started by WoodsBagot, Oct 22, 2014.

  1. WoodsBagot

    WoodsBagot

    Joined:
    Mar 6, 2014
    Posts:
    8
    What is the simplest way to toggle on and off the visibility of a piece of geometry in a scene via a keyboard press? Ideally, press it once for on, and once more for off.

    My object's name is "Object_1".

    Scripting newbie, so any help you can give would be super helpful.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Can you attach the script directly to the mesh renderer?
    Code (csharp):
    1. /// Toggle renderer on this.gameObject when key is pressed down.
    2. public class ToggleRenderer : MonoBehaviour
    3. {
    4.   void Update()
    5.   {
    6.     if (Input.anyKeyDown)
    7.       renderer.enabled = !renderer.enabled;
    8.   }
    9. }
     
    Last edited: Oct 22, 2014