max pleaner
max pleaner

Reputation: 26788

Only first NavMeshAgent can be placed

I am having an issue where only the first NavMeshAgent can be created.

First of all, I selected all mesh renderers for the NavMesh bake objects:

enter image description here

I fine-tuned the bake a bit so it had just the right amount of detail. The black gaps around the various objects would be larger if I used a larger radius in the baked agent options:

enter image description here

I have two spawners placed on either side of the back ramp (the one with the white sphere in it). These are invisible but in a script, NavMeshObjects are placed onto these locations on an interval and have their destinations set to one of the other gameobjects.

The first one works fine, but none of the other 17 NavMeshObjects in the spawner queue can have their destinations set.

public class MinionSpawner : MonoBehaviour {
    public void spawn (GameObject minion, GameObject target_position_obj) {
        // Find the target position from the passed gameobject
        MinionTargetPosition target_position = target_position_obj.GetComponent<MinionTargetPosition>();
        if (!target_position) { throw new System.Exception("no valid target position given"); }
        // Instantiate the given minion at the spawner's origin point
        GameObject new_minion = Object.Instantiate(minion, transform.position, new Quaternion(0,0,0,0));
        new_minion.SetActive(true);
        // Mark the minion at it's target position, for lookup upon collision or elsewhere
        MinionAttributes minion_attrs = new_minion.GetComponentInChildren<MinionAttributes>(true);
        if (!minion_attrs) { throw new System.Exception("object passed as minion does not have MinionAttributes"); }
        minion_attrs.target_position = target_position;
        // Configure a NavMeshAgent to navigate the minion from origin to target position.
        NavMeshAgent nav_mesh_agent = minion_attrs.gameObject.GetComponent<NavMeshAgent>();
        if (!nav_mesh_agent) { throw new System.Exception("minion doesn't have a nav mesh agent"); };
        nav_mesh_agent.Warp(transform.position);
        // ================================================================
        // THIS LINE FAILS:
        nav_mesh_agent.destination = target_position.transform.position;
        // ================================================================
        // mark the minion's position as occupied in the turret's dictionary
        Turret turret = target_position.turret;
        turret.minion_slots[target_position] = true;
        // set the minion's parent to the minion spawner, for organization's sake.
        minion.transform.parent = transform;
    }
}

I'm getting the error "SetDestination" can only be called on an active agent that has been placed on a NavMesh. and I know there are questions out there about this. But I've tried really everything I've found suggested, including baking the NavMesh, setting the NavMeshAgent's initial position with Warp, and making sure the points are on the mesh.

Upvotes: 0

Views: 403

Answers (1)

max pleaner
max pleaner

Reputation: 26788

The issue was something unrelated to the NavMeshagent. It was this line:

 minion.transform.parent = transform;

I needed to change it to:

 new_minion.transform.parent = transform;

Basically minion is the object that gets cloned and new_minion is the clone. So if I set the parent of minion, I am messing it up for future clones, I guess.

Upvotes: 0

Related Questions