GameObject stuck in scene, even when out of scope

If I create a GameObject, and then that GameObject goes out of scope, it is still visable in the hierarchy indefinitely, indicating that it was not destroyed.

I have the following class:

class A : MonoBehaviour {
	void Awake() {
		GameObject g = new GameObject();
	}
}

It seems that after I instantiate that class, there will forever be a copy of an empty GameObject floating in my scene. Can I remove this GameObject, other than by manually destroying it? If so, how?

Sorry if this is an asinine or frequently asked/answered question, I just feel like I must be missing something and I’m not finding the answer. It doesn’t make sense that there wouldn’t be a way to detect this dead object, or that it would remain in play after becoming seemingly unattached and out of scope.

If I just have to be really careful about my GameObjects, that’s fine I guess, but it seems a bit annoying that they don’t appear to be picked up by Garbage Collection because they’re referenced by something under the hood in Unity until they are manually destroyed. If this is actually the case, what is the reason for it? I’ll be perfectly happy to learn that there’s some awesome pattern this is intended to support that I haven’t thought of! =)

Thanks in advance!

If I create a GameObject, and then that GameObject goes out of scope, it is still visable in the hierarchy indefinitely, indicating that it was not destroyed.

It’s not out of scope, the unity state machine is quite aware of the MonoBehavior and the locally scroped GameObject you instantiated. The concept of scope doesn’t apply to the Unity Objects, i.e. i created a local int and the method is done being invoked shouldn’t it be marked to be collected by the Garbage collector.

It seems that after I instantiate that class, there will forever be a copy of an empty GameObject floating in my scene. Can I remove this GameObject, other than by manually destroying it? If so, how?

Yes until you remove it, kill the game engine or load another scene without attributing to your gameobject(s) to be Not destroyed.

Sorry if this is an asinine or frequently asked/answered question, I just feel like I must be missing something and I’m not finding the answer. It doesn’t make sense that there wouldn’t be a way to detect this dead object, or that it would remain in play after becoming seemingly unattached and out of scope.

What would make this a dead object? Nothing having a reference to it? we know thats not the case as Unity Engine manages all of this and it’s not indicative of just C# code you’re using. If you need to add properties that indicate this is a “dead” object, then you will need to code against that and implement it. Unity has many helper functions for finding GameObject of whatever type.

If I just have to be really careful about my GameObjects, that’s fine I guess, but it seems a bit annoying that they don’t appear to be picked up by Garbage Collection because they’re referenced by something under the hood in Unity until they are manually destroyed. If this is actually the case, what is the reason for it? I’ll be perfectly happy to learn that there’s some awesome pattern this is intended to support that I haven’t thought of! =)

You’re creating something in a game engine, you should always be careful about what you’re doing, instantiating and how much. This will most likely translate out to better performance then just willy-nilly creating whatever, whenever you want and lets just have unity take care of something through some process that actually doesn’t exist.

If you really want unity to kill/destroy an object after x period of time that you didn’t carry a reference for, then use the timing overload of Object.Destory(defaults to 0.0f if you don’t set it), if you decide to instantiate a gameobject but don’t carry the reference, don’t expect it to disappear after since Unity will definitely have a reference to this object regardless.