Question by
Patrick2607 · Jun 01, 2017 at 06:41 PM ·
instantiateprefabparametersconstructor
How can I pass a reference to an Instantiated object?
I want to store the Player object in an object he created (like a unit or a building). Usually I would do something like this:
public Player Player;
public Building(Player p) // Constructor of Building Class
{
Player = p;
}
How can I achieve the same behaviour when Instantiating MonoBehaviours? I'd like it all in one line. I know I can do it like this:
Building building = (Building) Instantiate(buildingPrefab, Vector.zero, Quaternion.identity);
building.Initialize(player); // Pass the player to set it
Is there a better way to do this? I need the Player reference in the Building because when I create units in the building I can easily assign the Player to it. (Which has the same problem).
Comment