- Home /
Question by
oliver-jones · Feb 09, 2012 at 10:42 PM ·
transformvector3variable
Assign Transform Variable With A Vector?
Hello,
I have a variable called target that is a Transform that has been assigned in the inspector. Is there a way to assign the variable with a Vector3 position?
if(FindClosestEnemy()){
target = FindClosestEnemy().transform;
}
else{
print("No Targets Found");
target = Vector3(0,0,0);
}
Something like that?
Comment
Best Answer
Answer by aldonaletto · Feb 09, 2012 at 11:15 PM
No way! They are completely different types. The easiest alternative would be to create an empty game object at 0,0,0 (let's call it "Target0") and assign it to target:
var target: Transform; static var target0: Transform; // make sure only one Target0 object exists ... if(FindClosestEnemy()){ target = FindClosestEnemy().transform; } else{ print("No Targets Found"); if (!target0){ // if target0 not yet created... target0 = new GameObject("Target0").transform; // create it! target0.position = Vector3.zero; // make sure it's at 0,0,0 } target = target0; // set target to the Target0 object }Declaring target0 as static makes the variable unique for all instances of this script, thus only one "Target0" will be created when needed.