Passing a Variable to an Instance


I know there have been a lot of posts on this subject, and I have read at least half a dozen, and I just don’t get it. The whole GetComponent deal is lost on me. I have a feeling because most of the posts reference something like GetComponent(ScriptHere) and I don’t know which script to put there. Plus the examples I have seen haven’t discussed if the code should go in the source or destination file. I am a NOOB to Unityscript, but I’ve coded in many other languages before, and I can’t wrap my head around this one bit.

Here is a the source file where I have made a button to Instantiate a prefab called MakeSpellObject.js.

var prefab:GameObject;
var spellName;

function OnGUI () {
	if (GUI.Button (Rect (20,40,90,20), "Create Spell")) {
		var pos = Vector3(13,2,0); 
		spellName = Instantiate(prefab, pos, Quaternion.identity);
		var spell = "spellPlay";
		Debug.Log("variable is set to " + spell);


The destination file is called DragSpell.js, and here is an excerpt of the relevant code.

function OnMouseDown () {
canMove = true;
gravitySetting = myRigidbody.useGravity;
freezeRotationSetting = myRigidbody.freezeRotation;
myRigidbody.useGravity = false;
myRigidbody.freezeRotation = freezeRotationOnDrag;
yPos = myTransform.position.y;

GetComponent("MakeSpellObject").spell = GetComponent("MakeSpellObject").spell;



If someone would take a look at my code that would be incredible.


wasn’t quite sure what you meant exactly by ‘passing parameters’ to instantiate. i assume you mean parameters for setting up the object, aside from what you’d pass into Instantiate, i.e. position, rotation, etc. So here’s a few different things I might do on a given usage of Instantiate in my code:

var obj=GameObject.Instantiate(prefab) as GameObject;
obj.transform.parent=transform; //makes the new obj a child of this one
obj.renderer.enabled=false; //disables the object initially until enabled later, i.e. if pre-caching objects

var fooCom:Foo=obj.GetComponent(Foo);

,Instantiate will return the newly created GameObject for you. In strict typing, you’ll have to add “as GameObject” to explicitly cast it because Instantiate returns just the bottom-level Object interface.

Once you have the GameObject reference, you can call GetComponent on that, passing in the name of the script file(don’t use quotes if you can help it, it’s faster) that is this other component you are trying to get. If the prefab you instantiated has the script attached, you’ll get a reference to that script, and you can then call methods and access vars from it like it’s a C++ class.

OK, So I got the code to work based off of what Tom posted, problem is, the variable was brought into the code that instantiated the instance of the prefab, but not into the instance itself. Here is the revised code for MakeSpellObject.js script.

function OnGUI () {

if (GUI.Button (Rect (20,40,90,20), "Create Hex")) {
	var pos = Vector3(13,2,0);
	var spellName = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject;
	spell = spellName.GetComponent("DragSpell").spellArray[0];


And then I put this array code in the DragSpell.js script file.

var spellArray = new Array (“spellBubble”, “spellLoop”, “spellPlay”, “spellStep”, “spellStill”);

So when I did Debug.Log(spellArray[0]) in the MakeSpellObject.js it came in beautifully so the GetComponent(“DragOject”); code worked, however it didn’t exist in the instance that was created. So I have had some success, but the end result is still not accomplished of passing a dynamic value of the spellArry into an instanced object. Anyone have any ideas if that is possible? I’m starting to think not.