- Home /
How to disable gameobjects in code?
Hello there, my friends! I was having a bit of trouble deactivating gameobjects, which really was rather inconvenient! In the Unity scripting reference page, they claimed that this piece of code would deactivate a gameobject as soon as the scene was loaded:
gameObject.SetActive (false);
However, I was rather disappointed when I discovered that the code was completely ineffective! What a shame! Well, I looked all over the place, and simply couldn't find out how to do it! How silly! So if anyone knows how I can create a boolean variable, which controls another different gameobject from being activated, please tell me, it would be greatly appreciated!
Many thanks indeed, and may you all have a jolly spiffing day!
Taliho!
Also, I forgot to mention - assu$$anonymous$$g that I fix this problem, would it deactivate the gameobject and its children, or just the object itself?
Tally ho!
Are you using Unity 3 or the new Unity 4 ?
It's a tad different in each ...
gameObject.SetActive() works for me completely as expected. Want to share your code?
For some reason gameObject.SetActive works fine for me. Try gameObject.active = false (it is the old way of doing it even though you get a warning saying it is obsolete but just try it anyway) Hope it helps you in someway ;)
Answer by brimby · Aug 27, 2014 at 05:53 PM
I think things have changed since the original question was asked, because SetActive() works fine for me. And conversely, the enabled property and the function SetActiveRecursively() do not exist on GameObject anymore.
I have also had the same issue. The enabled property no longer exists and SetActive() should be used ins$$anonymous$$d.
@DZapdos then you should up my answer so people see that the other answers here are out of date.
Answer by Theacesofspades · Jan 02, 2013 at 01:17 AM
use
enabled = false;
or
SetActiveRecursivly (false);
Answer by FirePlantGames · Oct 24, 2014 at 11:43 PM
This should work...
function Start()
{
gameObject.SetActive(false);
}
Your answer
Follow this Question
Related Questions
How do I set a reference in the inspector 1 Answer
How to active/deactive gameobject? 1 Answer
how to make my gameobjects disable and enable? 2 Answers
How to make an open and close box?,How to make open and close a box? 0 Answers
If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers