My scene has 2 tank prefabs with navmeshagent. What I want to do is tellling one tank prefab to stop when a certain condition is true. The problem is that when I change the agent.speed it affects both prefabs.
Thanks in advance
My scene has 2 tank prefabs with navmeshagent. What I want to do is tellling one tank prefab to stop when a certain condition is true. The problem is that when I change the agent.speed it affects both prefabs.
Thanks in advance
theres not much info about your particular situation, but maybe you could use Instantiate and store the tanks agents in an array and then access the agent you wish by array index. example script that instantiates a few tanks and toggles the speed of first tank in the array on left mouse click:
using UnityEngine;
using System.Collections;
public class Navtest : MonoBehaviour
{
public GameObject tank;
NavMeshAgent[] agents;
void Start ()
{
agents=new NavMeshAgent[5];
for(int i=0;i<agents.Length;i++)
{
GameObject g = Instantiate(tank);
agents[i]=g.GetComponent<NavMeshAgent>();
}
}
bool changeSpeed;
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
changeSpeed=!changeSpeed;
if(changeSpeed)
{
agents[0].speed=11;
}
else
{
agents[0].speed=0;
}
}
}
}
Thanks for your reply. One question though. Should I put this script on a warfactory object ? Because if I put this on my prefab it will enlessly create a new tank prefabs.