Is there any comprehensive list of what is available and accessible on deactivated game objects versus active game objects?
Or if there’s some features that are common pitfalls or may be less intuitive that is available or not when deactivated?
It seems like I can access any public property or run public methods as long as I have a reference to the deactivated game object or component while stuff like built in update methods, animation, physics and rendering is suspended.
But it would be good to have a list of what to expect. Perhaps some components work differently?
I did search but didn’t find any good listing (manuals or web) and I had to correct chatGPT 4 so I don’t quite trust the list it gave me …
I’m not sure what you’re referring to. Everything is available on them if you have a reference. They are just not rendering and the Components on them also considered disabled so Unity message methods don’t get calls (Update and similar).
They are also left out of the common searches like GameObject.Find. You can find at the methods is they include inactive game objects or they have the option to do so is discussed in the method’s manual. But none of these are “on the game object”.
Thanks, this is what I would expect but I was just curious because in the past I’ve found that I couldn’t access some things I thought I should be able to. Perhaps I was just using the wrong type of reference similar to Find and when asking chatGPT I got this “Scripts: Scripts are not active. Public methods in these scripts cannot be called, except for some specific Unity lifecycle methods like Awake() which are called when the GameObject is reactivated.”
before correcting it.
So I was wondering if I’ve gotten this “deactivate gameobject” wrong or if there were some exceptions.
The reason is that ChatGPT is often frequently delusional in its responses.
As a new user it will be almost impossible for you to tell when ChatGPT is wrong.
It’s far better to simply avoid ChatGPT and instead, always refer to the official documentation.
It may take some extra searching but you will at least be working from a known base that we can all agree upon.
Remember: you will NOT learn everything in a single session. Learning comes from iterative daily preparation and repetition of small core fundamentals.
No worries. I promise you I’m well aware of the delusion of chatGPT. I see it more as an alternate “you could try this” engine. But I didn’t find any extensive description other than “not active in the scene” from the manual and most stuff I found dealt with other things so before coming here I posed the question and saw at least one error.
But back to the subject, there should be no reason then that any public method or property in components or child/parent should be inaccessible due to an inactive game object unless using a method like gameObject.Find that specifically only returns active objects?
But in practice I rarely deactivate a gameObject with a script on it, and certainly not if I have to think for more than 2 seconds whether it will be an issue. The script goes on the empty parent, and the model and colliders and such – anything that may need to be dealt with individually – go onto children to be turned on and off.
Just to confirm your statement: trying to StartCoroutine() when the GameObject is inactive produces an error and returns null. If you deactivate a GameObject with running Coroutine(s), no further calls are made and all references to those Coroutines will eventually equate to null.
MonoBehaviours which are not enabled, however, have no bearing on whether Coroutines are serviced or not. You can start a Coroutine and then disable the component, or even start a Coroutine while the component is disabled, and you will get no Update() calls while happily servicing the Coroutine as long as the GameObject is active.
public class TestCoroutine: MonoBehaviour
{
void Start()
{
//gameObject.SetActive(false);
this.enabled = false;
Coroutine job = StartCoroutine(MyRoutine());
print($"Start made a job: {(job != null)}");
}
void Update()
{
print("Update()");
}
IEnumerator MyRoutine()
{
while (true)
{
print("MyRoutine()");
yield return null;
}
}
}
Just additional information that may or may not be related: Whether or not a MonoBehaviour has the checkbox in the editor to enable or disable it depends on which methods you add to the MonoBehaviour. Awake() is not called on inactive MonoBehaviours in a scene. If an exception is thrown from an Awake() method, then the MonoBehaviour will become disabled. UnityEngine objects like MonoBehaviour can be destroyed, and Unity overloads the == operator to make those object equal to null. So MyMonoBehaviour == null could be true when (object)MyMonoBehaviour == null would be false. This affects things like the null coalescing operators, like ?, ?., ??, and ?= when the references are UnityEngine types.