- Home /
Setting a variable to an instantiated object.
I am instantiating a box so that it appears when I kill an enemy. I am using this code to check to see if I am close to the box; if I am then the box will be destroyed. My only problem is that I don't know how to set healthDrop to the object I'm instantiating.
function Update ()
{
if (healthDrop)
{
var distance = Vector3.Distance(healthDrop.position, transform.position);
if (distance < 2)
{
Destroy("healthDrop")
}
}
}
Answer by Xtro · Jan 28, 2014 at 08:10 PM
I'm just giving the logic. Didn't check my code on the compiler.
var Object = GameObject.Instantiate(Prefab);
var Script = Object.GetComponent("ScriptName");
Script.Variable = any value;
The problem is that I instantiated the box in a separate script than the one I'm trying to destroy it in.
Where ever you create it, you need to get its reference, find the script on it and change the member value.
Can you please mark the correct answer if you fixed that problem?
I eventually did something else but this was the logic I was looking for thank you.