gameObject.SetActive (false); not working

I’m having a really strange problem where I’m trying to set gameObject.SetActive (false), but the GameObject remains onscreen. I used a print statement to determine that this part of the code is reached while running the game, so it’s not that the code isn’t being executed.

Also, replacing gameObject.SetActive (false); with Destroy(gameObject) does work, and the GameObject is removed from the screen. What could be causing this and how do I fix it?

I’m using C#, if that’s relevant.

It’s working now and I’m not sure what I did to fix it. I think it was just that the script wasn’t attached to the right GameObjects.

you are constantly activating the gameObject in a script attached to the GameObject.
for example somewhere in
Update()
{
gameobject.SetActive(true);
objectActive == true;
}
fix it with a boolean check variable i.e
if (objectActive == true)
{ object.SetActive(false);}