Ok I am beginning work on an inventory system that will allow players to pick up and place blocks and other items. Since each object in the inventory will represent an in game object I would like to reference the prefab that is cloned when the object is in game.
For example: The player has five bricks. The inventory slot might be an array where the first Slot holds the number of objects in that inventory slot (Slot = 5), and Slot holds the prefab that should be spawned when the block is placed (Slot = BrickPrefab).
Basically I am trying to store the template from which to spawn an item, not an actual in game item which would be an instance. Is this possible? If so how?
Very possible, just do the following:
create an empty scene
add a gameObject & attach the prefab factory to it
public class PrefabFactory : MonoBehaviour
public Transform PrefabTemplates;
public GameObject Create(int Id, Vector3 OurPosition, Quaternion OurOrientation)
return Instantiate(PrefabTemplates[Id], OurPosition, OurOrientation) as Transform).gameObject;
drag your prefab(s) from the project window into the prefabTemplates array
Call PrefabFactory.Instantiate(Id, Position, Orientation);
Ok got it figured out. Thanks to Weenchehawk, from your code I figured out that “Transform” is the type of variable that can store a prefab as a template instead of storing an instance. For example:
var MyTemplate : Transform;
public Transform MyTemplate;