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
4
Question by supermodular · Apr 02, 2012 at 05:49 PM · gameobjectinstantiateprefabvariabletype

What is the type of my prefab?

I'm confused about the type of objects in my scripts. I have a prefab called Enemy, with a script called EnemyScript. Unity seems pretty happy when I pass the prefab in with type EnemyScript. But isn't that just one component of Enemy? What would happen if there were multiple scripts associated with Enemy -- could I bring in the prefab with any of these types? Could I set the type of the prefab to simply be Enemy?

When my script assigns a value to transform, shouldn't it really be setting transform of the gameObject with which the script is associated? Or it that what's happening, and Unity is just being clever and doing that mapping for me? It feels weird that sometimes I refer to an object by one of its scripts. Likewise, sometimes it seems like I'm meant to refer to an object by its transform.

So what is the type of my prefab? Is it a gameObject, any script in the prefab, the name of the prefab itself, a transform, or something else? And if more than one of these is feasible, what are the implications of the various options?

Any help would be much appreciated. Thanks very much in advance.

Comment
Add comment · Show 1
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 shanusmagnus · Feb 02, 2013 at 04:04 PM 0
Share

Good job crafting such a clear and thorough expression of the question.

2 Replies

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

Answer by Bunny83 · Apr 02, 2012 at 06:27 PM

Basically it's always a GameObject, but you can use any reference to a component on that Gameobject as well. Components can't live without it's host GameObject. If you clone (instantiate) a component, it will clone the whole host GameObject with all it's Components and child objects.

When declaring a variable of type EnemyScript you can drag the gameobject on this variable but the editor will search for a component of that type and assign the reference to the component instead of the reference to the GameObject.

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 oukichi · May 31, 2019 at 06:41 AM 0
Share

It is very helpful answer.

avatar image
0

Answer by Lednar · May 28, 2018 at 08:41 PM

To give some extra details, I saw in Unity manual in Instantiating Prefabs at runtime a code that says:

 // Require the rocket to be a rigidbody.
 // This way we the user can not assign a prefab without rigidbody

 public Rigidbody rocket;
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

7 People are following this question.

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

Related Questions

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Retrieving GameObjects from a Prefab 1 Answer

Spawning a prefab at another object's location 3 Answers

I cant delete multiple instantiated prefabs 0 Answers

Set Rigidbody variable to Prefab using code 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