- Home /
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!
Answer by KiraSensei · Aug 25, 2013 at 12:48 PM
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.
If I have to place allot of these gameobjects down, should I clear out the variable? If I clear out the variable, will that delete the game object?
The goal here is to instantiate an object and have it store in it's script the last hit object my mouse was over. ("lastHitObj")
I'm going to be placing allot of these structures down and each one needs to store whatever the last hit object was when I instantiated it.
But do you really need game objects ?
$$anonymous$$aybe you can do what ever you want to do just with one script attached to the player that will have variables with the objects the mouse hits ?
What do you really want to do ? If you only need to stock a list of game objects hit by the mouse, you should have one script attached to the player, you don't need other game objects.
Basic program$$anonymous$$g books would tell you the principle of scope.
When declaring between two {} the variable is destroyed when reaching the }
if ( something ) {
if ( somethingElse ){
// Create obj
var obj = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + ObjectOffset, Quaternion.identity );
} <-obj is destroyed
obj.member = value; <-error, obj does not exist in this scope
}
What I mean is that you can create thousands of objects inside the if statement, the reference won't make it out of the }.
It's a tower defense type situation;
I have a grid of sprites on the ground, side by side. Each sprite, it's own object of course. The sprite objects have the tag "open". If I mouse click over a sprite with the tag "open" i can drop a gameobject on it. But now I need the sprite to be tagged "closed" so that I no longer can drop units on that same sprite.
If the gameobject stores the sprite it's on as a variable, it can tell it to tag itself as "open" before that gameobject gets destroyed.
I could achieve this through functions like OnTriggerStay, but because their are so many sprites and the potential of a ton of game units, I'm trying to avoid using a call that happens every frame, such as "OnTriggerStay".
thanks for clearing that up fafase!
Hrm, still having trouble with this! Here's where I'm at;
if ( Input.Get$$anonymous$$ouseButtonDown (0) && lastHitObj )
{
if ( lastHitObj.tag == "placementPlane_Open" )
{
if ( structureIndex == 0 )
{
var myObjectOffset : Vector3 = new Vector3( 0.1f, 0.5f, 0);
var myObject : GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position + myObjectOffset, Quaternion.identity );
myObject.tilePartner = lastHitObj;
}
lastHitObj.tag = "placementPlane_Closed";
lastHitObj.renderer.material = original$$anonymous$$at;
}
}
this gives me the error;
BCE0019: 'tilePartner' is not a member of 'UnityEngine.GameObject'.
"tilePartner" is a GameObject variable inside of the script component attach to "myObject".
What am I missing here?
Answer by Rick74 · Aug 26, 2013 at 06:18 AM
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!