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
0
Question by theredace · May 01, 2014 at 09:15 PM · rigidbodyinstantiateprefab

Instantiating a prefab as a component type?

I'm confused by the ability to instantiate an object as a component type, like a Rigidbody. For example, take Unity's own instantiate tutorial here: http://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate

It's the standard "instantiate a rocket when you fire" example. It makes perfect sense to me that your rocket would be a prefab of a gameobject with attached components like rigidbody, collider, etc. But I don't understand what you're doing when you instantiate a prefab and cast it into a Rigidbody. How is that cloned object different than just a gameobject with a rigidbody attached?

That tutorial basically explains that instantiate returns a type called "object", and that even if the prefab you're instantiating has a Rigidbody component attached, your cloned object will NOT "be" a rigidbody. Makes no sense to me. I instantiate prefabs as just GameObjects all the time and they act exactly like they should with the components already attached to the prefab.

Comment
Add comment · Show 3
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 JChilton · May 01, 2014 at 10:07 PM 2
Share

When you instantiate anything, it is actually an Object you're creating based off another object. What actually gets duplicated is the gameObject, with all components attached to it. Rigidbodies, Transforms, colliders, monobehaviours and more are all Components. $$anonymous$$ost people cast it to GameObject after instantiating because it's shorter to call gameObject.GetComponent than (component).gameObject.getComponent. Casting as a Rigidbody just means that you now have a direct refernece to that component, as opposed to casting as a Gameobject then getting whatever components you required. There is no difference what you cast to or as, just changes how you're referencing your new object.

avatar image theredace · May 01, 2014 at 10:14 PM 0
Share

Okay, so if I understand correctly, in the Unity tutorial video where they instantiate a Rigidbody, they're actually instantiating a gameobject but creating a reference specifically to the Rigidbody component on that gameobject so that they can do things a bit quicker like myRigidbody.AddForce as opposed to having to create a separate reference to the Rigidbody component.

But in essence, it's no different than just instantiating the gameobject and then doing further references to it's components if necessary. Is that it?

avatar image NoseKills · May 02, 2014 at 01:26 PM 1
Share

Yeah if you look at the example they give, the source Object they are Instantiating is of type Rigidbody. We can see that the Instantiate does clone the whole GameObject and all of it's components, but depending on what you used as the source, Instantiate returns that component.

They could as well have implemented the method in a way that it always returns the GameObject and you would have to use GetComponent to get access to the Rigidbody, but they thought it's more convenient this way.

2 Replies

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

Answer by NoseKills · May 01, 2014 at 10:42 PM

The short answer is: Because that's the way Unity and method Instantiate() was designed to work internally.

If you play with the Instantiate a bit, you notice that when you're instantiating from a Rigidbody, you always get a Rigidbody, and a GameObject from a GameObject.

In the Unity Instantiate tutorial you're referring to, they are instantiating from a Rigidbody, and in that case Instantiate returns a Rigidbody.

All the basic building blocks you attach to your GameObjects in Unity, Rigidbody, Colliders, Joints... extend the class UnityEngine.Component which in turn extends the class UnityEngine.Object. Since Instantiate can clone any Object, it can logically also clone any Component. When it is used to clone a Component, it clones the GameObject the component was attached to too, and all other Components that were attached to the GameObject.

The reason Instantiate returns a Rigidbody when you clone from a Rigidbody and a GameObject when you clone a GameObject is simply because that's how it was designed.

They could've also "overloaded" the Instantiate method for each subclass of Object too but they decided to make just one method that can clone and return any subclass of Object and leave the casting to the right type to us users.

Comment
Add comment · Show 1 · 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 theredace · May 02, 2014 at 02:39 PM 1
Share

Great explanation. Thank you!

avatar image
1

Answer by Jinxology · May 01, 2014 at 09:56 PM

The way I understand it, you're simply working with the RigidBody component of a GameObject. I believe the structure is always a GameObject containing components such as Transform and RigidyBody. You'll see the same thing with the Transform component. People often use Transform instead of GameObject to Instantiate or link if that's the data they will be using on the GameObject.

From the Instantiate reference:

If you are cloning a Component then the GameObject is[sic] is attached to will also be cloned, again with an optional position and rotation.

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

26 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

Related Questions

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Instantiated prefab doesn't collide with player 0 Answers

Would a spawnpoint with the subcomponent of RigidBody cause instantiate problems? 2 Answers

I cannot get my code to function correctly, and do not know what to do. Please help. 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 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