2
\$\begingroup\$

I am trying to get a GameObject to navigate through a map and get to the exit. I have created a navmesh for the map and given the player GameObject a navmesh agent. The exit is marked with a empty GameObject. Using the unity manual I made this script and atached it to the player:

function Start () 
{
    var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
    agent.SetDestination(GameObject.Find("exit"));
}

The porblem is that this script gives the error

"Assets/Scripts/enemy.js(4,29): BCE0017: The best overload for the method 'UnityEngine.NavMeshAgent.SetDestination(UnityEngine.Vector3)' is not compatible with the argument list '(UnityEngine.GameObject)'."

I think is is because unity wants a vector3 but I have given it a GameObject. How can I get this to work with a game object?

\$\endgroup\$
2
\$\begingroup\$

You are passing in a GameObject, yet the function wants a Vector3.

To fix this, you could pass in transform.position:

function Start () 
{
    var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
    agent.SetDestination(GameObject.Find("exit").transform.position);
}

Function reference here.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.