• 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 /
avatar image
2
Question by Jeff Ciaccio · Jul 01, 2010 at 12:41 PM · instantiate

setting properties when instantiating

I have a script on an object in a prefab called beanBagProperties that simply has several instance variables that I want to assign upon instantiation.

The controller object has a script with a function, InstantiateBeanBag(), and that script has several variables like playerNo, roundNo, tossNo, etc that have the same variable name as those in the beanBagProperties script.

How do I assign the state of the variables in the controller to the instance variables in the beanBagProperties script.

Would it be something as simple as this?

function Instantiate()//or is this a no-no b/c it's a reserved word???
{
  Instantiate(prefabToInstantiate, Vector3(0, 1, 0), Quaternion.identity);
  prefabToInstantiate.playerNo = this.playerNo;
  prefabToInstantiate.roundNo = this.roundNo;
}

OR... do I need to specify the script within the new object like this:

function Instantiate()//or is this a no-no b/c it's a reserved word???
{
  Instantiate(prefabToInstantiate, Vector3(0, 1, 0), Quaternion.identity);
  prefabToInstantiate.beanBagProperties.playerNo = this.playerNo;
  prefabToInstantiate.beanBagProperties.roundNo = this.roundNo;
}
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

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by duck · Jul 01, 2010 at 01:05 PM

No, your script won't work as written, because you're trying to set variable values on the prefab itself, rather than on the instance which was just created.

The Instantiate function returns a reference to what was just created, so in theory you could do this:

var newBeanBag = Instantiate(prefabToInstantiate, pos, rot);
newBeanBag.someVariable = someValue;

...as long as the prefabToInstantiate is a reference to your script on the prefab, not the prefab gameobject itself. For example:

Correct:

var prefabToInstantiate : YourScriptName;

Incorrect:

var prefabToInstantiate : GameObject;
// or
var prefabToInstantiate : Transform;

This is because the "Instantiate" function returns a reference to the type of object from which it was created - although it still always builds a complete new instance of the prefab including its mesh, transform & scripts attached.

So if your prefab reference variable's type is actually the type of script that you want to manipulate on your prefab (i.e. the name of your script), then you'll get back a direct reference to the new instance of the script on the new prefab. All put together, it would look like this: (I have renamed your script "BeanBag" in this example, to be consistent with unity's conventions. You should do the same - rename the file "BeanBag").

var beanBagPrefab : BeanBag;

function SpawnBeanBag() { var newBeanBag = Instantiate( beanBagPrefab, pos, rot ); newBeanBag.playerNo = this.playerNo; newBeanbag.roundNo = this.roundNo; }

If your prefab reference variable's type is "Transform" or "GameObject", then you'll get back a reference to the new instance's Transform or GameObject respectively. In this case you would have to use "GetComponent" to then grab the reference to the script, like so:

var newBeanBag = Instantiate( beanBagPrefab, pos, rot );
var theScript = newBeanBag.GetComponent(BeanBag);
theScript.someVariable = someValue;

So you can see that by using your script's type as the variable type in the first place, you can completely avoid this step of having to instantiate and then grab the reference to the script on the new instance.

Of course if you are most interested in getting back the transform of your prefab, rather than the script on it, using Transform as the prefab variable type is completely fine. However it's worth remembering that if you did use your script as the prefab type, you can access its transform, gameobject and other component properties directly, like this:

newBeanBag.transform
newBeanBag.gameObject
newBeanBag.renderer
newBeanBag.rigidbody
// etc

And yes it's best not to use Instantiate as the name of your own function. It's possible to use the same name in multiple places but it means you have to add prefixes to specify which function you're talking about, like "Object.Instantiate" to differentiate it from "this.Instantiate".

Comment
Add comment · Show 3 · 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 Jeff Ciaccio · Jul 01, 2010 at 01:07 PM 0
Share

Thanks Duck! Is there a tutorial with something like this that shows how to set instance variables upon instantiation??

avatar image duck ♦♦ · Jul 01, 2010 at 01:10 PM 0
Share

um, not sure, but I just told you everything you need to know. I'll add a fuller example. just a mo.

avatar image duck ♦♦ · Jul 01, 2010 at 01:15 PM 0
Share

ok, done. Enjoy!

avatar image
1

Answer by Case23 · Jul 01, 2010 at 12:55 PM

Use GameObject.GetComponent function.

var script : ScriptName = gameObject.GetComponent(ScriptName);

script.playerNo = 50;

Comment
Add comment · Show 1 · 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 Jeff Ciaccio · Jul 01, 2010 at 01:03 PM 0
Share

So in my case would it become this? function Instantiate()//or is this a no-no b/c it's a reserved word???{

var script: specificBeanBag = gameObject.GetComponent("beanBagProperties");

Instantiate(prefabToInstantiate, Vector3(0, 1, 0), Quaternion.identity);

specificBeanBag.playerNo = this.playerNo;

specificBeanBag .roundNo = this.roundNo;}

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Checking if object intersects? 1 Answer

Create Clones as children in loops? 1 Answer

how to do something based on how many game objects exist 2 Answers

instantiate works on one but not other object 0 Answers

Help Guitar Hero Style or similar 0 Answers


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