Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Oct 23, 2015 at 07:45 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
13
Question by Drakestar · May 17, 2012 at 08:09 PM · gameobjectinstantiateparametersconstructor

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
19
Best Answer

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 :)

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hathol · May 18, 2012 at 02:47 PM 0
Share

That code goes into the script of the object you want to create of course and can be called like

YourComponent bla = YourComponent.Create();

avatar image Drakestar · May 20, 2012 at 05:39 AM 0
Share

+1, been toying around with a similar idea.

avatar image Guirao · Jan 17, 2013 at 11:27 AM 1
Share

Do you have to assign the script to the prefab?

avatar image svendkiloo · Dec 05, 2018 at 07:25 AM 2
Share

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.)

avatar image Ziplock9000 · Mar 01, 2019 at 03:33 PM 1
Share

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.

avatar image svendkiloo Ziplock9000 · Mar 04, 2019 at 07:33 AM 0
Share

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.

Show more comments
avatar image
6

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;
 
 }
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Drakestar · May 18, 2012 at 03:03 AM 0
Share

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.

avatar image Drakestar · May 18, 2012 at 03:09 AM 0
Share

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.

avatar image syclamoth · May 18, 2012 at 03:40 AM 1
Share

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.

avatar image MithosAnnar · May 18, 2012 at 08:05 AM 0
Share

you could just send multiple messages. I do because sending messages is fast and neat.

avatar image syclamoth · May 18, 2012 at 08:19 AM 0
Share

Sending one message is still faster than sending several.

Show more comments

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I pass a reference to an Instantiated object? 0 Answers

ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer

How do I get an instantiated button to active/de-active a gameobject 0 Answers

Instantiate differents objects [C#] 1 Answer

Instantiate as child for gameobjects with tag 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges