Thanks, still got the same problem with the wave.
wave config code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = “Enemy Wave Config”)]
public class WaveConfig : ScriptableObject
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpwans = 0.5f;
[SerializeField] float spawnRandomFactor = 0.5f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] float moveSpeed = 2f;
public GameObject GetEnemyPrefab()
{ return enemyPrefab; }
public List<Transform> GetWaypoints()
{
var waveWaypoints = new List<Transform>();
foreach (Transform child in pathPrefab.transform)
{
waveWaypoints.Add(child);
}
return waveWaypoints;
}
public float TimeBetweenSpans()
{ return timeBetweenSpwans; }
public float RandomFactorSpawn()
{ return spawnRandomFactor; }
public int NumberOfEnemis()
{ return numberOfEnemies; }
public float MoveSpeed()
{ return moveSpeed; }
}
Enemypathing code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPathing : MonoBehaviour
{
[SerializeField] WaveConfig waveConfig;
[SerializeField] List waypoints;
int waypointIndex = 0;
// Start is called before the first frame update
void Start()
{
waypoints = waveConfig.GetWaypoints();
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update()
{
Move();
}
public void SetWaveConfing(WaveConfig waveConfig)
{
this.waveConfig = waveConfig;
}
private void Move()
{
if (waypointIndex <= waypoints.Count - 1)
{
var targetPosition = waypoints[waypointIndex].transform.position;
var movementThisFrame = waveConfig.MoveSpeed() * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
}
}
else
{
Destroy(gameObject);
}
}
}
enemyspawner code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] List<WaveConfig> waveConfigs;
[SerializeField] int startingWave = 0;
[SerializeField] bool looping = false;
// Start is called before the first frame update
IEnumerator Start()
{
do
{
yield return StartCoroutine(SpawnAllWaves());
}
while (looping);
}
private IEnumerator SpawnAllWaves()
{
for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
{
var currentWave = waveConfigs[startingWave];
yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}
}
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
for (int enemyCount = 0; enemyCount < waveConfig.NumberOfEnemis(); enemyCount++)
{
var newEnemy = Instantiate
(waveConfig.GetEnemyPrefab(), waveConfig.GetWaypoints()[0].transform.position, Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().SetWaveConfing(waveConfig);
yield return new WaitForSeconds(waveConfig.TimeBetweenSpans());
}
}
}
Many thanks