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 Uukrul · Mar 25, 2015 at 01:03 PM · instantiateprefabmonobehaviourinstanceaddcomponent

Associate objects to a prefab

This question has probably been asked several times in several different ways. I'm fairly new to Unity, but an experienced coder. My question is best described by an example:

I have a Player : MonoBehaviour object and I'd like to be able to associate it with another MonoBehavior (e.g. sword, helm, etc...). The player object has member variables that will hold the instances of those objects (e.g. ISword, IHelm, etc...) I'm having trouble identifying a pattern for instantiating these peripheral objects. I can either Instantiate(...) the object, but then it must be referenced somewhere in the scene hierarchy beforehand OR I can <>.gameObject.AddComponent(), but this will result in a new instance each time.

The latter would make more sense in this case. However, if I wanted to spawn a sword into the world, it seems like I would need some general container to hold it. I guess, that could be a chest or vendor.

I'm just looking for some general guidance here, I'm sure this has been solved 100 times already.

Thanks ahead of time :)

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
1

Answer by Landern · Mar 25, 2015 at 01:08 PM

Prefabs and references to them after the project is built are just serialized data representing the prefab at whatever state it was in when referenced(in the inspector).

A lot of times people will use either a GameManager object that has disabled objects ready to instantiate or Object Pools that have a range of hidden and instantiated objects ready for use and they recycle them in and out when needed. The point of assigning in the inspector is to have the cookie cutter for instantiation, if you're project is on a PC/Mac, you probably won't feel much pain instantiating on the fly, on mobile this can be come an issue depending on how often you instantiate and the quality of the objects in general.

Other then the object pool, the general idea is to use Prefabs and manipulate then when instantiating/reusing during that particular frame(setting up the game object).

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 Uukrul · Mar 25, 2015 at 05:11 PM 0
Share

Thank you, that helps clarify the issue for me. So let's say I have a Game$$anonymous$$anager object that contains all the classes of objects that I would want to instantiate in the game. So to spawn a sword, I would simply use the sword reference that was added to the Game$$anonymous$$anager at 'compile time' and instantiate it at run time:

public Sword sword;

Instantiate(sword); //or something like that

Now if I've created several instances of that sword, how do I access a specific instance and assign it to a Player prefab? GetComponent(...) will return an instance, but how do I identify a specific instance?

Thanks again.

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

2 People are following this question.

avatar image avatar image

Related Questions

What is the difference between grey and blue prefab ? 1 Answer

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

How do I get a CS script in assets to be a stored monobehaviour variable 1 Answer

How do I get the global position 2 Answers

(Similar Prefabs) vs (Single Prefab + AddComponent) 2 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