❔ Tower Defense Enemy Movement

Having a problem with line 37....
1
2 using UnityEngine;
3
4 [RequireComponent(typeof(Enemies))]
5 public class EnemyMovement : MonoBehaviour
6 {
7 //public static Transform [] waypoints;
8 private Transform target;
9
10 private int wavepointIndex = 0;
11
12 private Enemies enemy;
13 private object waypoints;
14
15 private void Start()
16 {
17 enemy = GetComponent<Enemies>();
18
19 target = (Transform)waypoints;
20 }
21
22 private void Update()
23 {
24 Vector3 dir = target.position - transform.position;
25 transform.Translate(enemy.speed * Time.deltaTime * dir.normalized, Space.World);
26
27 if(Vector3.Distance(transform.position, target.position) <= 0.4f)
28 {
29 GetNextWaypoint();
30 }
31
32 enemy.speed = enemy.startSpeed;
33 }
34
35 private void GetNextWaypoint()
36 {
37 if(wavepointIndex >= Waypoints.- 1)
38 {
39 EndPath();
40 return;
41 }
42 else
43 {
44 wavepointIndex++;
45 target = Waypoints.points [wavepointIndex];
46 }
47 }
48
49 private void EndPath()
50 {
51 PlayerStats.Lives--;
52 WaveSpawner.EnemiesAlive--;
53 Destroy(gameObject);
54 }
55 }
56
1
2 using UnityEngine;
3
4 [RequireComponent(typeof(Enemies))]
5 public class EnemyMovement : MonoBehaviour
6 {
7 //public static Transform [] waypoints;
8 private Transform target;
9
10 private int wavepointIndex = 0;
11
12 private Enemies enemy;
13 private object waypoints;
14
15 private void Start()
16 {
17 enemy = GetComponent<Enemies>();
18
19 target = (Transform)waypoints;
20 }
21
22 private void Update()
23 {
24 Vector3 dir = target.position - transform.position;
25 transform.Translate(enemy.speed * Time.deltaTime * dir.normalized, Space.World);
26
27 if(Vector3.Distance(transform.position, target.position) <= 0.4f)
28 {
29 GetNextWaypoint();
30 }
31
32 enemy.speed = enemy.startSpeed;
33 }
34
35 private void GetNextWaypoint()
36 {
37 if(wavepointIndex >= Waypoints.- 1)
38 {
39 EndPath();
40 return;
41 }
42 else
43 {
44 wavepointIndex++;
45 target = Waypoints.points [wavepointIndex];
46 }
47 }
48
49 private void EndPath()
50 {
51 PlayerStats.Lives--;
52 WaveSpawner.EnemiesAlive--;
53 Destroy(gameObject);
54 }
55 }
56
9 Replies
ero
ero2y ago
and what's the error?
whiteviperx
whiteviperxOP2y ago
'-' cannot be applied to operands of type 'Transform[]' and 'int'
ero
ero2y ago
so there you go
whiteviperx
whiteviperxOP2y ago
yes but how do I solve it?
ero
ero2y ago
don't apply '-' on an operand of type 'Transform[]'
whiteviperx
whiteviperxOP2y ago
I want the code to look somethings like this but this didnt work either

private void GetNextWaypoint()
{
if(wavepointIndex >= Waypoints.points.Length - 1)
{
EndPath();
return;
}


private void GetNextWaypoint()
{
if(wavepointIndex >= Waypoints.points.Length - 1)
{
EndPath();
return;
}

ero
ero2y ago
and why didn't it work?
whiteviperx
whiteviperxOP2y ago
causes problems in the waypoint scripts I think i have to start over on the scripts... I'm just trying to get the enemy to follow waypoints and point in the correct direct at each turn
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server