Instantiate GameObject with parameters

How do I instantiate a GameObject with parameters? In traditional programming parlance, how do I new a GameObject with a non-parameter-less constructor?

Obviously I can do this:

GameObject go = Instantiate(Resources.Load("Spot")) as GameObject;
go.transform.position = new Vector3(42, 42, 42);

But updating all the properties individually is tedious and requires the writing of many setters. Is there a way to instantiate with parameters, akin to

Spot go = new Spot(new Vector3(42, 42, 42);

And - ideally - is there a way to “overload that constructor” to allow for different instantiation scenarios? Is there a way to pass along parameters to the objects constructor or to the Start() function?

try that:

public static Object prefab = Resources.Load("Prefabs/YourPrefab");
public static YourComponent Create()
{
	GameObject newObject = Instantiate(prefab) as GameObject;
    YourComponent yourObject = newObject.GetComponent<YourComponent>();

    //do additional initialization steps here

	return yourObject;
}

and then overload and parametrize your create function as much as you like :slight_smile:

Easy Peasy.

public GameObject spot;

public Vector3 v = new Vector3(42, 42, 42);

GameObject go = Instantiate(spot, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;

go.SendMessage("TheStart", v);

On the script attached to your spot:

void TheStart (Vector3 v) {   // you can't use start. But this is just as good.

    transform.position = v;

}