Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Disabling game object

Discussion in '2D' started by sdebussc, Nov 10, 2019 at 11:34 PM.

  1. sdebussc

    sdebussc

    Joined:
    Sep 7, 2017
    Posts:
    2
    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

    MisterSkitz

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

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