My Enemies stopped moving

After the ObjectPool script and lesson my enemies stop moving on path. Have tripled checked the code compare to instructor but i cant find the issue. I got Enemy Health and Enemy Mover scripts on the Enemy Prefab, i got Objectpool script on the Objectpool gameobject.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ObjectPool : MonoBehaviour

{

    [SerializeField] GameObject enemyPrefab;

    [SerializeField] float spawnTimer = 1f;

    [SerializeField] int poolSize = 5;

    GameObject[] pool;

    void Awake()

    {

         PopulatePool();  

    }

    void Start()

    {

        StartCoroutine(SpawnEnemy());

    }

    void PopulatePool()

    {

        pool = new GameObject[poolSize];

        for (int i = 0; i < pool.Length; i++)

        {

            pool[i] = Instantiate(enemyPrefab , transform);

            pool[i].SetActive(false);

        }

    }

    void EnableObjectInPool()

    {

        for (int i = 0; i < pool.Length; i++)

        {

            if(pool[i].activeInHierarchy == false)

            {

                pool[i].SetActive(true);

                return;

            }

        }

    }

    IEnumerator SpawnEnemy()

    {

        while(true)

        {

            EnableObjectInPool();

            yield return new WaitForSeconds(spawnTimer);

        }

    }

}

and this is the enemy mover script.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EnemyMover : MonoBehaviour

{

    [SerializeField] List<Waypoint> path = new List<Waypoint>(); //We serialize and we drop in the path we want for the enemies

    [SerializeField] [Range(0f, 5f)] float speed  = 1f;

   

    // Start is called before the first frame update

    void onEnable()

    {

        FindPath();

        ReturnToStart();

        StartCoroutine (followPath());

    }

    void ReturnToStart()

    {

        transform.position = path[0].transform.position;

    }

    void FindPath()

    {

        path.Clear();

        GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");

        foreach (GameObject waypoint in waypoints)

        {

            path.Add(waypoint.GetComponent<Waypoint>());

        }

    }

    IEnumerator followPath()

    {

        foreach (Waypoint waypoint in path)

        {

            Vector3 startPosition = transform.position;

            Vector3 endPosition = waypoint.transform.position;

            float travelPercentage = 0f;

           

            transform.LookAt(endPosition); // Auto einai gia na koitaei o enemy panta pros ta pou paei

            while (travelPercentage < 1f)

            {

                travelPercentage += Time.deltaTime * speed;

                transform.position = Vector3.Lerp(startPosition , endPosition , travelPercentage);

                yield return new WaitForEndOfFrame();

            }

        }

        gameObject.SetActive(false);

    }

}

Hi byronara,

Welcome to our community! :slight_smile:

Are there any error messages in your console? Maybe a NullReferenceException error? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Make sure all method names are spelt correctly because C# is case-sensitive. Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

I hope this helped. :slight_smile:


See also:

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

Privacy & Terms