- Home /
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?
Answer by Sequence · Nov 11, 2013 at 01:50 AM
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.
Answer by landon912 · Nov 11, 2013 at 02:30 AM
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! :)
Is it needed that 'TempTEST' inherits from $$anonymous$$onoBehaviour for the attached gameobject to work?
No, but then you'll need to call "Start()" through another means and assign objToSpawn in code.
Right, and that call has to be done from the main thread. So from any other $$anonymous$$onoBehaviour callback. You can't create new GameObjects from a constructor of a class or from any other thread.
If you already know which components to add, you can do the following: var objToSpawn = new GameObject("Name of new object", typeof(BoxCollider), typeof($$anonymous$$eshRenderer), ...); // and so on
Answer by narqs · Jan 09, 2017 at 01:59 PM
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.
oh sorry i didn't get your comment :) i know what a prefab is, and i meant an empty game object. but for some reason i didn't think about it when i posted to your comment, xD well you're absolutly right it dosn't really make any sense to have an empty prefab. sorry for the confusion :)
oh and i just edited my answer rena$$anonymous$$g it EmptyObj.
Answer by SouthernCoder69 · Nov 11, 2013 at 03:54 AM
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.
Yeah, I'm struggling to think of a sensible use case for an empty prefab.
a reason for an empty prefab, could be if you have an array of gameobjects which needs to be children of an empty prefab, so that you could organize your gameobjects. a good example of this would be object pooling.
i would however make the empty prefab private and handle everything in code. :)