Problems in movement of enemy after enabling in object pool

SO i have created an object pool for ‘n’ number of enemies. I want to enable each enemy on the basis of a time period and as soon as the enemy is enabled, it will find a path, complete its run and return back to the object pool & reset (Disable and reset path and coords).

But as soon as i run the game, the object pool is formed but the enemies are not moving. Where did i go wrong?

using System.Collections.Generic;
using UnityEngine;

public class EnemyMovementScript : MonoBehaviour
{    
    List<Waypoint> path = new List<Waypoint>();
    
    [Range(0f, 10f)]
    [SerializeField] float speed;

    void onEnable()
    {
        findPath();
        returnToStart();
        StartCoroutine(FollowPath());
    }

    void returnToStart()
    {
        transform.position = path[0].transform.position;
    }

    void findPath()
    {
        GameObject parent = GameObject.FindGameObjectWithTag("path");

        foreach(Transform child in parent.transform)
        {
            path.Add(child.GetComponent<Waypoint>());
        }
    }

    IEnumerator<WaitForEndOfFrame> FollowPath()
    {
        foreach(Waypoint waypoint in path){

            Vector3 startPosition = transform.position;
            Vector3 endPosition = waypoint.transform.position;
            float travelPercentage = 0;

            transform.LookAt(endPosition);

            while(travelPercentage < 1f)
            {
                transform.position = Vector3.Lerp(startPosition, endPosition, travelPercentage);
                travelPercentage += Time.deltaTime * speed;
                yield return new WaitForEndOfFrame();
            }
        }
        gameObject.SetActive(false);
    }
}

This is my movementScript for movement of enemies

It would be helpful to see your script for pooling as well as the script that instantiates/retrieves your enemies. My best guess is that while the enemies are created in the pool waiting for you, that they’re not getting activated and sent into the world.

Is this related to a specific course?

Hi @Rustic_Dude,

I assume this is for the Realm Rush project in our Unity 3D course, isn’t it?

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? And what is the onEnable method? Your own one or did you want to use Unity’s OnEnable method? If you want to use Unity’s methods, pay close attention on the correct spelling because C# is case-sensitive.


See also:

I used onEnable because I want it to run only when the entity (enemy) is enabled. This is a script found only on the enemy

It’s related to realm rush from complete gamedev course. I will send the code of the object pool as well

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour
{
    [SerializeField] GameObject enemy;
    [SerializeField] int poolSize;
    [SerializeField] float timePeriod = 0;

    GameObject[] pool;

    private void Awake()
    {
        pool = new GameObject[poolSize];
        populatePool();
    }

    void Start()
    {
        // StartCoroutine(activateEnemy());
    }

    void populatePool()
    {
        for(int i = 0; i < pool.Length; i++)
        {
            pool[i] = Instantiate(enemy, transform);
            pool[i].SetActive(false);
        }
    }

    IEnumerator activateEnemy()
    {
        while (true)
        {
            for(int i = 0; i < pool.Length; i++)
            {
                yield return new WaitForSeconds(timePeriod);
            }
        }
    }
}

Where do you call onEnable? Since this is your own method, it must be called somewhere. Otherwise, the code will not get executed, and the enemies won’t move.

No no onEnable is not my own method. I made a mistake in naming convention. Its supposed to be
void OnEnable()

1 Like

Does your code work as expected with OnEnable()?

1 Like

Yes it is working now
Thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms