Home > Software design >  Enemies aren't moving in my tower defense game in Unity 3D C#
Enemies aren't moving in my tower defense game in Unity 3D C#

Time:12-01

I have recently started using Unity and decided to follow Brackey's Tower Defense Game tutorials on Youtube. After following part 2, (https://www.youtube.com/watch?v=aFxucZQ_5E4&list=PLPV2KyIb3jR4u5jX8za5iU1cqnQPmbzG0&index=2) the enemy does not move. The strange thing is that there are no errors that show in the Unity console. I only have 2 scripts, which are shown below.

Waypoints script:

using UnityEngine;

public class Waypoints : MonoBehaviour {

   public static Transform[] points;

   void Awake ()
   {
        points = new Transform[transform.childCount];
            for (int i = 0; i < points.Length; i  )
        {
            points[i] = transform.GetChild(i);
        }
   }

}

Enemy script:

using UnityEngine;

public class Enemy : MonoBehaviour {

    public float speed = 10f;

    private Transform target;
    private int wavepointindex = 0;

    void Start ()
    {
            target = Waypoints.points[0];
    }

    void update ()
    {
        Vector3 dir = target.position - transform.position;
        transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);

        if (Vector3.Distance(transform.position, target.position) <= 0.4f)
        {
            GetNextWaypoint();
        }
    }

    void GetNextWaypoint()
    {
        if (wavepointindex >= Waypoints.points.Length - 1)
        {
            Destroy(gameObject);
            return;
        }
        wavepointindex  ;
        target = Waypoints.points[wavepointindex];
    }

}

I have made sure that the Waypoints script is only in the parent called 'Waypoints' which contains all of the Waypoints. My Enemy script is added to the enemy in my scene. I have checked with the Youtube tutorial multiple times to make sure the code is the same. The Waypoints script is running before the Enemy script. I have made sure of this by going to Edit > Project Settings > Script Execution Order. The enemy just stays still. Nothing seems to work, please help me. Thanks.

CodePudding user response:

I think this is because your update() function is lowercase.

Try write it like this:

void Update(){
}
  • Related