destination is not a member of Unity.GameObject

Hello, im having a problem with my nav mesh agent, i get an error that says: " ‘destination’ is not a member of ‘Unity.GameObject’ "

the gameobject that this script is attached to is an prefab so its instantiated in to the game, so i need to automaticly declare the “target” in the script but it doesnt work…

Any help is welcome!

#pragma strict

var target: Transform;
var speed: float;
var agent : GameObject;
	
function Start ()
{
	var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
	target = GameObject.Find("TownHall").transform;
}
	
function Update ()
{
	var relativePos = target.position - transform.position;
	var rotation = Quaternion.LookRotation(relativePos);
	var step = speed * Time.deltaTime;		
	transform.rotation = rotation;
    agent.destination = target.position; 
}

You’ve declared ‘agent’ a GameObject in the class. You also declared ‘agent’ a NavMeshAgent in Start() which is Destroyed as soon as Start() finished running. So what you are left with in Update is an empty gameObject ‘agent’ with which destination is definitely not a member.

I think your code should look like this

 #pragma strict
 
 var target : Transform;
 var speed : float;
 var agent : NavMeshAgent; //Changed from GameObject to NavMeshAgent 
     
 function Start ()
 {
     agent =  GetComponent.<NavMeshAgent>(); //Removed 'var' and ': NavMeshAgent'
     target = GameObject.Find("TownHall").transform;
 }
     
 function Update ()
 {
     var relativePos = target.position - transform.position;
     var rotation = Quaternion.LookRotation(relativePos);
     var step = speed * Time.deltaTime;        
     transform.rotation = rotation;
     agent.destination = target.position; 
 }