Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Corbeau · Sep 09, 2011 at 08:08 PM · gameobjectprefabruntimeaddcomponent

Adding Prefab Components

I need to add a prefab gameobject as a component to another gameobject via scripting, but am having problems. I've tried using the AddComponent function and been able to add blank gameobjects this way. I've also been able to add various custom scripts this way. However, I have been completely unable to add prefab gameobjects in this fashion. Neither a type parameter nor a string parameter will find any prefabs, and AddComponent won't take straight references (unlike Instantiate).

This is a major problem for my project; it seems like prefab components ought to be a logical, integral functionality of unity's prefab system, but I cannot find a command to make it work. Does anyone know how to do this?

Thanks,

C.

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

1 Reply

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

Answer by PrimeDerektive · Sep 09, 2011 at 08:26 PM

A Prefab is a GameObject. a Component is anything (MonoBehaviours mostly) that attaches to GameObjects. Therefore, a Prefab cannot be a Component, and could never be used as a parameter in AddComponent().

Are you confusing "adding a Prefab to a GameObject" with parenting a prefab to a GameObject? Because that's as simple as setting the transform.parent, eg:

myNewObject : GameObject = Instantiate(myPrefab, pos, rot);
myNewObject.transform.parent = gameObjectIWantToParentTo.transform;
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 Corbeau · Sep 11, 2011 at 08:11 PM 0
Share

This is correct, thanks. I was confused about the difference between the two types of hierarchy.

However, I have a related problem: I don't want or need to instantiate the modular object at the time of assembly. I just need to store it for later access and instantiating. Setting a prefab's parent obviously doesn't work, but instantiating before setting the parent relationship is undesirable. Would you happen to know if there is a way to create a gameobject, without instantiating it in the scene, that is a copy of a prefab?

Right now I'm setting up scripts that will store assembly info, then recreate the object when needed. It would be far more elegant to store complete GameObject references.

Thanks,

C.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

grab all game objects with a distance of the object? 2 Answers

Prefab (Project Folder) to a GameObject (on the Scene) 2 Answers

Auto Build Script 2 Answers

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

transforming a prefab randomly at runtime 3 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