How do I assign a var to the object I've just instantiated?

The object I instantiate has a var: GameObject that I would like to assign an object too. But I’m not sure how this works in Java script.

The code I have so far;

if ( Input.GetMouseButtonDown (0) && lastHitObj )
   			if ( lastHitObj.tag == "placementPlane_Open" )
   				if ( structureIndex == 0 )
   					var ObjectOffset : Vector3 = new Vector3( 0.1f, 0.5f, 0);
   					Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );					

   			lastHitObj.renderer.material = originalMat;
     		hudOn = false;
     		bigUnit = false;

So immediatly after I’ve instantiated the object, I wanna say something like; InstantiatedObject.variable = lastHitObj

I hope this makes sense!

The first thing to do is to get the instantiated object in a variable :

var myInstantiatedObject:GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );

I assume here that allStructures[structureIndex] is a GameObject, so the instantiated object is a GameObject too… See the doc here.

Then, to valuate a variable “in” the game object, you need to attach a script to it first. You have several way : first you can have one already in the prefab used ti instantiate the game object (so allStructures[structureIndex] needs to have a script), or you can create your script on runtime with AddComponent. This method returns the added component, you will need it after, so keep it in a variable :

var myObjScript:MyScript = myInstantiatedObject.AddComponent(MyScript);

In this script, you need a variable (I assume of type “GameObject” again) that will contain lastHitObj. I assume you already know how to do that …

Finally, in the script you put in your question you will be able to do :

myObjScript.variable = lastHitObj;

You cannot put variables directly on a game object, only in scripts attached to a game object.

So after a bit more research online, and some thumbing through old tutorial codes I found the answer! (which might have already been said), but I’ll just type it out so theirs no confusion;

this line;

myObject.tilePartner = lastHitObj;

needed to be changed to this;

myObject.GetComponent(myObjectScript).tilePartner = lastHitObj;

sigh…works perfectly now.

Thanks everyone for helping me to get there!