Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

Hi everyone,

I have been trying to get this enemy script to work for the past few days now. The enemy will spawn into the scene then it finds the way points and then adds them to the array so it can follow there transforms like a way point system. If any one can help I keep getting an error. When the enemy gets to the last way point I want them to self destroy.

Error:(Assets/Enemy1.js(25,23): BCE0022: Cannot convert ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’.)

#pragma strict
var WayPoint1 : GameObject; 
var WayPoint2 : GameObject;
var WayPoint3 : GameObject;
var WayPoint4 : GameObject;
var WayPoint5 : GameObject;

var waypoint : Transform[];
var speed : float = 5;
var currentWaypoint : int;
var CanMove : boolean = false;

function Awake ()
{
	//Find Way Points on Awake
	WayPoint1 = GameObject.Find("1");
	WayPoint2 = GameObject.Find("2");
	WayPoint3 = GameObject.Find("3");
	WayPoint4 = GameObject.Find("4");
	WayPoint5 = GameObject.Find("5");
}
function Start ()
{
	//Add Found WayPoints to Array
 	waypoint[0] = WayPoint1;
 	waypoint[1] = WayPoint2;
 	waypoint[2] = WayPoint3;
 	waypoint[3] = WayPoint4;
 	waypoint[4] = WayPoint5;
}

function Update () 
{
	if(CanMove == true)
	{
		if(currentWaypoint < waypoint.length)
		{
			var target : Vector3 = waypoint[currentWaypoint].position;
			var moveDirection : Vector3 = target - transform.position;
			var velocity = rigidbody.velocity;
	 
			if(moveDirection.magnitude < 1)
			{
				currentWaypoint++;
			}
			else
			{
				velocity = moveDirection.normalized*speed;
			}
		}
		rigidbody.velocity = velocity;
	}
}

You should get rid of all the separate waypoint variables, and the Start and Awake functions. All you need is the “var waypoint : Transform;” variable. Drag the waypoints onto this array.

First thing I notice is that you’ve got WayPoint1 through 5 defined as GameObjects, yet you are trying to add them to an array of Transforms called waypoint in your Start function.

Perhaps this would work better:

function Start ()
{
   //Add Found WayPoints to Array
   waypoint[0] = WayPoint1.transform;
   waypoint[1] = WayPoint2.transform;
   waypoint[2] = WayPoint3.transform;
   waypoint[3] = WayPoint4.transform;
   waypoint[4] = WayPoint5.transform;
}