- Home /
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 :)
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).
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
Follow this Question
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