I’m trying to make a turret that shoots a game object with the tag “targetObject”, however I keep on getting the same error and thanks to MonoDevelop’s very unclear debugging system, I’m not sure how to fix it. (I am new to Unity) Thanks!
#pragma strict
var turretsOn : boolean = true;
private var targetPosition : Vector3;
function Start () {
aimStart();
}
function aimStart() {
//Just a loop :)
while (turretsOn === true) {
//We only want the turret to aim every second, for perfromance and gameplay.
yield WaitForSeconds(1);
targetPosition = GetClosestObject("targetObject");
Transform.LookAt(Vector3(targetPosition));
}
}
function GetClosestObject(tag:String) : GameObject {
var objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
var closestObject : GameObject;
for (var obj : GameObject in objectsWithTag) {
if(!closestObject) {
closestObject = obj;
}
//Compares distances
if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position)) {
closestObject = obj;
}
}
return closestObject;
Just like the error is telling you, your function
function GetClosestObject(tag:String) : GameObject
returns a GameObject and you are trying to assign that gameObject into a Vector3 variable
private var targetPosition : Vector3;
If you are trying to store the position of that gameObject, reference it’s position vector and store that
targetPosition = GetClosestObject("targetObject").transform.position;