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;
}

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

Use GameObject.GetComponent function.

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

script.playerNo = 50;