The question is answered, right answer was accepted
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?
Answer by hathol · May 18, 2012 at 02:42 PM
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 :)
That code goes into the script of the object you want to create of course and can be called like
YourComponent bla = YourComponent.Create();
You don't, at least in newer Unity versions, have to GetComponent. If your prefab is of the type of the script (Resources.Load has a generic version), then Instantiate will return an object of the same type, so you'll have the script already. Just as a side note: Don't use Resources ;) (2.1. Best Practices for the Resources System: Don't use it.)
This does not instantiate the GO with parameters. In the instantiate call the object is fully created and awake is called. So any values you might have wanted to pass in as a construction parameters you can't. The object has already been made by the time you get to your GetComponent line.
I would post another answer, but as one has been suggested, I'm not allowed (another reason Unity Answers is inferior to SO). One option, if the initialisation values are or could be serialisable, would be to set them on the prefab before instantiation. This isn't ideal, but it's a way around not being able to set custom values (other than position, scale and rotation) with the creation of a game object.
Answer by MithosAnnar · May 17, 2012 at 08:19 PM
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;
}
I'm assu$$anonymous$$g that you mean go.Send$$anonymous$$essage(). Cool, this works "well enough". I wasn't aware of this and kept retrieving the script via GetComponent first, which is cumbersome.
Looks like spoke too soon, as Send$$anonymous$$essage only allows one argument. There's hacks around this, but creating and extracting arrays isn't nearly as neat as actual ctor functionality.
I usually use 'messenger' classes that contain all the parameters I need. I then simply send the newly constructed 'messenger' as the single parameter of Send$$anonymous$$essage.
you could just send multiple messages. I do because sending messages is fast and neat.
Sending one message is still faster than sending several.
Follow this Question
Related Questions
How can I pass a reference to an Instantiated object? 0 Answers
instantiate prefab at every gameobject with tag 0 Answers
Parenting Instancing and Prefab issues 0 Answers
Using One Script for Two Separate Parent GameObjects Failing 0 Answers
Breaking Object by Destroying it and Instantiating Broken Parts problem, help! 2 Answers