new GameObject returns null

public class Test : MonoBehaviour
private GameObject _gameObject;

    void Reset()
        _gameObject = new GameObject();

I have no idea why it returns null only when I add a component to a gameobject, but for example, if I click Reset on it later on it will return a GameObject.

Also EditorUtility.CreateGameObjectWithHideFlags works fine.

UPDATE: I want to do so to create a folder-like GameObject to store all components that script needs. Also seems like it is an inconsistent behaviour, no idea what causes it (Unity version, project settings?).

As already pointed out this does not “instantiate” any object.
As a rule for most applications: Never use “new” when handling any Gameobject, transform or component! Use Instantiate when you want to create an object in your scene

If you want to add components to an gameobject by script this is done by using gameObject.AddComponent

As i said, as a general rule never use new for any monobehaviourrelated stuff.

You really don’t want to put gameobject generating code inside the editor only Reset callback. Especially you create the gameobject unconditionally. Furthermore you store the reference in a non serialized private variable. So this reference will not be saved at all. Finally there could be issues when / from where Unity calls the Reset method. Also that “ExecuteInEditMode” attribute looks like some abusive usage. “ExecuteInEditMode” is not meant for providing editor features. It’s just meant to have runtime features work during edit time (like particle effects, and stuff like that).

It’s not clear what you actually want to achieve. If the object this script is attached to requires another gameobject (for some reason), why don’t you make a prefab that includes that gameobject? If you really want to create the gameobject on the fly, just use some lazy loading whenever you actually need it.

private GameObject m_Obj = null;
public GameObject Obj
    get {
        if (m_Obj == null)
            m_Obj = new GameObject("tmpObj");
        return m_Obj;

Now whenever you need to work with your extra gameobject, just use the “Obj” property and it will create the gameobject on the fly if it hasn’t been created yet. Though be warned, as i said above, you should avoid things like that at edit time since the created gameobject will be part of the current scene and mark the scene dirty.

It would be much easier for us to help you if we know what you actually wanted to achieve. Editor functionality does not belong into MonoBehaviour scripts unless you really know what you’re doing.

I came across the same “feature”:
new GameObject() returns null when Reset gets called by adding a component in the inspector but works perfectly fine when clicking the reset button.

I found a workaround:

private void Reset()
     // calling new Gameobject on reset returns null. 
     // This is the hack I found to work best

IEnumerator CreateEmptyGameObject()
     yield return null;
     GameObject go;
     go = new GameObject("Empty game object");
     // now go has a reference to the game object as expected     
     // and you can code your stuff

I guess Unity doesn’t update the hierarchy cache quick enough?
Anyway, this works just fine.

Here is my solution.

void Reset()
	Invoke(nameof(Editor_Init), 0); // try not to use "string" type here,
	// instead we can use nameof() to label which function call we using.
	// process in next ZERO second is a hack, to run the same thing without error.

void Editor_Init()
	GameObject go = new GameObject("Sub-Folder");
	go.transform.SetParent(transform); // will work like this. ya~~~

Creating a gameObject will create a variable which is able to store a gameObject from the scene. When using ‘new’ you not actually instantiating a new object, you are basically doing nothing. Use instantiate() to create a new gameobject. You can use a oprefab for spawning a specific gameobject.