- Home /
 
               Question by 
               julinzp · Jan 29, 2019 at 11:46 AM · 
                instantiatenavmeshnavmeshagentpoolingpool  
              
 
              Pooled objects change behavior.
What I am trying to do is create a scene with humans walking around (crowd simulation). I wrote scripts to pool humans, each human has a NavMeshAgent on it and an AgentController script to follow the target, first pooled humans express the right behaviour, however the re-pooled don't follow the target. I wonder how could I solve this, i tried to set the AgentController script to enable, however it doesn't work. //Script to create avatars public class CreateAvatars1 : MonoBehaviour { public float hTime = 3f;
     void Start()
     {
         InvokeRepeating("Create", hTime, hTime);
     }
     void Create()
     {
         GameObject obj = Pooler1.current.GetPooledObject();
         if (obj == null) return;
         obj.transform.position = transform.position;
         obj.transform.rotation = transform.rotation;
         obj.SetActive(true);
         obj.GetComponent<NavMeshAgent>().enabled = true;
         obj.GetComponent<AgentController>().enabled = true;
         //obj.GetComponent<Animator>().Play("WALK");
     }
 }
 //Pooler  script
 public class Pooler1 : MonoBehaviour
 {
     public static Pooler1 current;
     public GameObject pooledObject;
     public int pooledAmount = 20;
     public bool willGrow = true;
     List<GameObject> pooledObjects;
     // Use this for initialization
 
     private void Awake()
     {
         current = this;
     }
     void Start()
     {
         pooledObjects = new List<GameObject>();
         for (int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             obj.SetActive(false);
             pooledObjects.Add(obj);
         }
     }
 
     public GameObject GetPooledObject()
     {
         for (int i = 0; i < pooledObjects.Count; i++)
         {
             if (!pooledObjects[i].activeInHierarchy)
             {
                 return pooledObjects[i];
             }
         }
         if (willGrow)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             pooledObjects.Add(obj);
             return obj;
         }
         else
             return null;
     }
 }
 
 //Script for following target
 public class AgentController : MonoBehaviour {
     public Transform target;
     NavMeshAgent agent;
     
     void Start () {
         agent = this.GetComponent<NavMeshAgent>();
         agent.SetDestination(target.position);
     }
     
 }
 
 
 
 
 
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                