Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Ben Ezard · Dec 27, 2012 at 01:53 PM · gameobjectinstantiate

Empty gameobject instantiation problem

Hi, when I instantiate a gameobject that I have created dynamically, e.g.

var p : GameObject = GameObject.Instantiate(new GameObject("CarrierControl"), Vector3.zero, Quaternion.identity);
there seems to be 2 instances created - "New Game Object" and "New Game Object(Clone)". At the moment it isn't a problem as I only have a few, but later on when I'm creating more, with lots of components attached, it could get to be a problem.
Anybody got any idea why this is happening? It doesn't happen when instantiating prefabs, only dynamically created objects.
Thanks
Comment
Add comment · Show 2
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 Fattie · Dec 27, 2012 at 02:11 PM 1
Share

Ben -- basically DO NOT USE "new GameObject"

that is only for making literally the component which is called "GameObject". it would only be for "technical use"

If you want to take a THING in your game (like "a monster" or "the spaceship") and make more of them, use Instantiate.

there is great confusion in Unity between "a game object" (ie, simply talking about "some THING" in your game) and literally a GameObject which is a special, um, technical thingy - heh.

avatar image Fattie · Dec 27, 2012 at 02:20 PM 1
Share

I've realized another source of confusion here

new GameObject("CarrierControl")

that just makes a new blank GameObject and happens to NA$$anonymous$$E it "CarrierControl"

it has utterly nothing to do with anything in your game that is currently called CarrierControl. okie dokey?

I'm guessing you want to COPY something you already have which is named CarrierControl. If so, you use Instantiate to do that.

3 Replies

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

Answer by AlucardJay · Dec 27, 2012 at 02:05 PM

Yep, you are creating 2 gameObjects =]

Instantiate is normally used for prefabs : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

new GameObject for making things on-the-fly : docs.unity3d.com/Documentation/ScriptReference/GameObject.GameObject.html

"New Game Object" is your new GameObject("CarrierControl")

"New Game Object(Clone)" is your Instantiate

So, pick a method. Do you want to create an object from an object referenced in that script, or do you want to create a new one and add components to it?

=]

Comment
Add comment · 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
0

Answer by ad48hp · Oct 11, 2013 at 03:55 PM

var p:GameObject = new GameObject();

Comment
Add comment · 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
0

Answer by advancexz · Oct 25, 2016 at 05:11 PM

just drop the 'new GameObject("CarrierControl")' from your code and use the prefab variable instead:

 public Transform prefabToCopy; //maybe assigned from your resources to your script in the component editor, or from resources from within the script
 
 private Transform prefabToSpawn;
     
 prefabToSpawn = Instantiate(prefabToCopy, Vector3.zero, Quaternion.identity);
Comment
Add comment · 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

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

12 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

Related Questions

Instantiating GO infront of player 2 Answers

Souls pickup mechanic (Like Dark Souls, BloodBourne) 0 Answers

UnityEngine.Object.Instantiate 1 Answer

what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers

Instantiate Terrain Object as child of Empty Game Object 1 Answer


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