Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Disabling game object

Discussion in '2D' started by sdebussc, Nov 10, 2019.

  1. sdebussc


    Sep 7, 2017
    I am using Unity 2019.2.8f1 and experiencing weird results when I try to disable a game object. I have an object with a Sprite Renderer, Polygon Collider 2D, and a custom script. In the OnMouseDown handler in the script, I disable the gameObject by calling:
    Code (CSharp):
    1. this.gameObject.SetActive(false);
    . When I play the scene, sometimes I have to click on the object twice before it will disappear. Other times, it will never disappear. If I disable the object manually in the inspector, the object is still displayed. It seems that there is a problem with the Unity editor/player. Has anyone experience this problem?

    To troubleshoot this problem further, I created a new Unity project with just a game object in the scene. Everything works as expected. The object disappeared when I clicked on it the first time.
  2. MisterSkitz


    Sep 2, 2015
    Try creating a variable for your game object you want to disappear.

    Code (CSharp):
    1. public GameObject yourObj;
    3. // Then in your MouseHandlerDown()
    5. yourObj.SetActive(false);
    Tell me if this works. If not, show more of your code so I can analyze your issue.