new GameObject(); is this a waste of space?

If I do something like this:

var v = new GameObject();
if (thing != null)
    v = Instantiate(thing);

Is this a waste of space (and a possible memory leak) if “thing” is null? Or does v get cleaned up when it goes out of scope?

When you call new GameObject(), a new GameObject is created and automatically added to the scene.

Even if you change what v is pointing to, the created GameObject remains in the scene.

You should do something like:

GameObject v ;
if( thing == null ) v = new GameObject();
else v = Instantiate(thing);

Or :

 GameObject v = (thing == null ) ? new GameObject() : Instantiate( thing ) ;