How do you create an empty gameobject in code and add it to the scene?

I’d like to create an empty gameobject dynamically from code and add it to the scene. I can’t seem find anything on how to do this, everything is just for spawning prefabs, which I don’t really want to do. I assume I can create a gameobject just by calling new GameObject(), but then how do I add that to the scene?

5 Answers

5

using UnityEngine;
using System.Collections;

public class TempTEST : MonoBehaviour {

    //store gameObject reference
    GameObject objToSpawn;

    void Start()
    {
        //spawn object
        objToSpawn = new GameObject("Cool GameObject made from Code");
        //Add Components
        objToSpawn.AddComponent<Rigidbody>();
        objToSpawn.AddComponent<MeshFilter>();
        objToSpawn.AddComponent<BoxCollider>();
        objToSpawn.AddComponent<MeshRenderer>();
    }
}

Here some free code! :slight_smile:

just what I needed :)

–

Is it needed that 'TempTEST' inherits from MonoBehaviour for the attached gameobject to work?

–

If you already know which components to add, you can do the following: var objToSpawn = new GameObject("Name of new object", typeof(BoxCollider), typeof(MeshRenderer), ...); // and so on

–

Thank you!

–

omg thank you that's what i needed i love you

–

I’m pretty sure the gameObject gets added automatically upon creation. (This is also why gameObject creation has to be done within the main thread.)

Looks like you're right. I didn't think it was being added because the parent of the object was always null, but it's getting collision events so it seems to be working.

–

null parent means it's in the root of the scene.

–

Hey late answer i know, judging from what you are trying to do, this could be helpfull

private GameObject EmptyObj;
public GameObject prefab;

void Start()
{
   EmptyObj = new GameObject("name");
   // in case you want the new gameobject to be a child
   // of the gameobject that your script is attached to
   EmptyObj.transform.parent = this.gameObject.transform;

   // and in case you want to throw other gameobjects into your new gameobject
   prefab.transform.parent = EmptyObj.gameObject.transform;
}

I know that there’s allready an answer, but this could be helpful to others.

Ah, that's not a prefab. This probably explains the confusion in comments above. Prefabs are GameObject templates, that you can use to instantiate other "prefabricated" GameObjects, which are then created with the same internal structure as the prefab. That's why the idea of an empty prefab makes little sense - if there's nothing in the template then one might as well just make an empty GameObject like you're doing. You are correctly showing how to create an empty GameObject in your scene, but calling it a "prefab" only confuses things.

–

I just did this in unity, so my answer is 100% solution - test for yourself and then please upvote.

  1. Create a empty game object (GameObject->Create Empty’

  2. Drag and drop this empty GameObject into your ‘Prefab’ folder in the projects/assets/. If you don’t have such a folder, create it.

  3. Delete the empty GameObject from the hierarchy

  4. Create a script with this language…

    using UnityEngine;
    using System.Collections;

    public class test : MonoBehaviour {

     public GameObject emptyGameObjectPrefab;
     
     void Start () {
     Instantiate(emptyGameObjectPrefab, transform.position,Quaternion.identity);
     	
     }
    

    }

  5. drag and drop the empty GameObject prefab onto the script variable in the inspector that we just created called ‘public GameObject emptyGameObjectPrefab’.

That’s it…step by step from start to finish. This is tested in Unity 4.2 as of 5 seconds ago.

Works, but it's easier to just do it with 1 line of code.

–

Omg thank you i love you i can give you some chocojuline

hey, i came across this and i'm having the same problem. what did you use to change the parameters for the scaling? i can't seem to figure it out

–