Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
15
Question by MrCrinkle · Nov 11, 2013 at 01:49 AM · gameobjectdynamiccreate

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

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.)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrCrinkle · Nov 11, 2013 at 03:07 AM 0
Share

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.

avatar image elenzil · May 11, 2015 at 09:09 PM 2
Share

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

avatar image
59

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! :)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lulax666 · Oct 17, 2015 at 07:23 AM 0
Share

just what I needed :)

avatar image Zarkow · Jan 01, 2016 at 12:41 PM 0
Share

Is it needed that 'TempTEST' inherits from $$anonymous$$onoBehaviour for the attached gameobject to work?

avatar image landon912 Zarkow · Mar 23, 2016 at 04:50 PM 1
Share

No, but then you'll need to call "Start()" through another means and assign objToSpawn in code.

avatar image Bunny83 landon912 · Mar 23, 2016 at 05:26 PM 1
Share

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.

avatar image Zendist · Oct 11, 2016 at 07:02 PM 3
Share

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

avatar image wlachenal · Feb 10, 2020 at 08:31 PM 0
Share

Thank you!

avatar image
5

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jan 09, 2017 at 08:49 PM 0
Share

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.

avatar image narqs Bonfire-Boy · Jan 09, 2017 at 09:10 PM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tpainton · Nov 08, 2015 at 05:47 PM 0
Share

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

avatar image Bonfire-Boy tpainton · Oct 11, 2016 at 07:08 PM 1
Share

Yeah, I'm struggling to think of a sensible use case for an empty prefab.

avatar image narqs Bonfire-Boy · Jan 09, 2017 at 02:12 PM 0
Share

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. :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Placing a cube on the face of another 4 Answers

Creating GameObject from asset. 4 Answers

Assign mouse events for gameobject/collider at runtime 1 Answer

Camera movement question 1 Answer

(solved)Pasing WWW data post/get to load gameobjects? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges