移动后删除navmesh组件

时间:2017-12-22 09:30:15

标签: unity3d

我创建了一个用于移动游戏对象的类:

public class Navmesh_move : MonoBehaviour {
    private NavMeshAgent _thismove;
    Transform test;
    //Vector3 destenation_Pos;  
    Vector3 destenation_Position,vector3test;
    // Use this for initialization
    public void move(float x,float y,float z)
    {

        //this.transform.Rotate (0, 0, 180);

        _thismove =this.gameObject.GetComponent<NavMeshAgent>();
        destenation_Position = new Vector3(x,y,z);
        _thismove.SetDestination(destenation_Position);
        NavMeshAgent tempnavmesh = new NavMeshAgent ();
        tempnavmesh =this.gameObject.GetComponent<NavMeshAgent> ();
        tempnavmesh.enabled = false;
        //this.transform.position=destenation_Position;

    }

}

然后我把它归结为一个对象游戏。

但在添加tempnavmesh.enabled = false;之前我的游戏对象移动正确。但在添加该行后,我得到"SetDestination" can only be called on an active agent that has been placed on a NavMesh.错误,我的游戏对象无法移动。

移动游戏对象后如何删除navmeshAgent组件???

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。 看起来像这样:

public void move(float x,float y,float z)
{

    //this.transform.Rotate (0, 0, 180);

    _thismove =this.gameObject.GetComponent<NavMeshAgent>();
    destenation_Position = new Vector3(x,y,z);
    _thismove.SetDestination(destenation_Position);
    Invoke ("disablenavmeshagent", 2);      
}

//-------------------------------------
void disablenavmeshagent()
{
    NavMeshAgent tempnavmesh = new NavMeshAgent ();
    tempnavmesh =this.gameObject.GetComponent<NavMeshAgent> ();
    tempnavmesh.enabled = false;
}