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.
Does this gameObject exist as a child of another parent gameObject? After calling gameObject.SetActive, what does gameObject.activeSelf return in a print statement?
Answer by Metaparadox · Mar 03, 2016 at 02:33 PM
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.
Answer by contact_sachin · Jul 30, 2018 at 10:41 AM
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);}
Your answer
Follow this Question
Related Questions
How to enable script on another gameobject in C#? 1 Answer
Instantiate() and Destroy() vs setActive() 1 Answer
Destroyed instance of Prefab, can't spawn it back. 1 Answer
Changing a Prefab's Text component seems to be broken. 1 Answer
I have a character object type that I want to make into a game object 0 Answers