- Home /
problem extracting transform from an array
hi all, this is a script to move an object in V3 towards a target object, the target object is randomly chosen from an array of object with a certain tag, the main issue is in my find target function but im lost, any help would be appreciated
#pragma strict
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
//var tower: GameObject;
var myTransform : Transform; //current transform data of this enemy
var targets : GameObject[] ;//hold an array of target objects
var target : Transform; //the enemy's target
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{//find all the game objects tagged as target
targets = GameObject.FindGameObjectsWithTag("target");
//if no target set run function find target
if(target==null){
findTarget();
}
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.transform.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
function findTarget(){
//randomly pick a target from the collected array
var newTarget : int = Random.Range( 0, targets.length );
//set the chosen randomly picked target as the curren target
target = targets[newTarget].transform.position;
}
Comment
Best Answer
Answer by robhuhn · Sep 25, 2013 at 01:11 PM
Your target is declared as Transform but you are trying to assign a Vector3 to it.
target = targets[newTarget].transform; //position removed
cheers robhuhn, im still new to this and get easily confused, that has fixed the problem, thanks very much