So I have set up 2 paths for my enemies to follow and prefabbed both, them and the enemies. They both have wave config files attached to them correctly and all the code is perfectly up to date, even so far as copying Gary’s code working perfectly as well. 2 problems I’m having are wrecking my brain a little and I’ve exhausted all of my options (including chatGPT)
So the first issue is that when the enemy spawns the 3 enemies (tried having 3 of the same prefab enemies, setting 3 different variants of the same prefab) but they’re all on top of eachother, tried adjusting the speed and spawn times and nothing helps.
The second issue I’m having is, nothing is following the second path (yellow is the first path where one single ship follows correctly, albeit multiple ships on top of eachother and the green diamonds mark the second path where nothing follow it at all), I can’t see anything set up differently.
I’m at a total loss here. As a novice I have no idea what I’ve done wrong.
Got the first part sorted now, thanks. As for the second part, I’ve added a new spawner and added path 1 wave config (which I’ve added the path 1 prefab of the green path) but now they all spawn from the centre and follow the same path (which looks like path 1).
Your Pathfinder scripts tries to find an enemy spawner, but it will only ever find one. It’s ignoring the second one.
What you should do is delete the second spawner again, and then follow the course. The multiple spawners is covered a little later - I think. You didn’t link this post to the correct lecture so I’m going by what’s in your code vs what’s in the repo
Oh okay, I’ll press on through lecture 118 and see how I go. As a newbie it’s a bit “oh no!” when something on screen happens for one person but not for you.
Please see the post I linked. Screenshots are not good. Sorry, I wasn’t clear
Anyway, here’s the problem
The ‘for loop’ is supposed to be inside the ‘foreach’ loop.
What your code is doing here is looping through each wave config and saying; ok, current wave is now this one. Then - when it’s done - the current wave is the last one in the list. Then only does it start spawning enemies. But only for the current wave, which is the last one.
When the for loop is inside the ‘foreach’, it will grab the first wave and start spawning enemies. Then it will wait a little, grab the next wave and start spawning that wave’s enemies. Etc.
My apologies, got it now! Won’t make that mistake again! I’ll try the parse in and see if that works better for you.
I still can’t get it to work, it’s closer to being right, the timing is right and it’s spawning both waves now but it still won’t spawn wave 1 (green) in the right place, it starts that in the centre.
This is the change to the code, not sure if I’m still slightly off in the code formatting or there’s something in the inspector or heirarchy I need to set but I’ve toggled everything. Worst comes to worst, i should be able to carry on with the lectures as I don’t think it’ll cause issues as such right, just won’t look right visually?
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] List<WaveConfigSO> waveConfigs;
[SerializeField] float timeBetweenWaves = 0f;
[SerializeField] WaveConfigSO currentWave;
void Start()
{
StartCoroutine (SpawnEnemyWaves());
}
public WaveConfigSO GetCurrentWave()
{
return currentWave;
}
IEnumerator SpawnEnemyWaves()
{
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
foreach (WaveConfigSO wave in waveConfigs)
{
Instantiate(currentWave.GetEnemyPrefab(i), currentWave.GetStartingWaypoint().position, Quaternion.identity,transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
currentWave = wave;
}
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
{
Instantiate(currentWave.GetEnemyPrefab(i), currentWave.GetStartingWaypoint().position, Quaternion.identity,transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
yield return new WaitForSeconds (timeBetweenWaves);
}
}
type or paste code here
Please show me Wave 1 in the scene. Expand it in the hierarchy and select it so that I can see all the points
Also, the SpawnEnemyWaves code is all wrong. This is what you have
IEnumerator SpawnEnemyWaves()
{
// loop through each enemy in the current (first) wave
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
// for each enemy, loop through the waves and spawn that enemy on each wave's start point
foreach (WaveConfigSO wave in waveConfigs)
{
Instantiate(currentWave.GetEnemyPrefab(i), currentWave.GetStartingWaypoint().position, Quaternion.identity,transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
currentWave = wave;
}
// loop through the enemies again and spawn another enemy on the last wave's start point
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
{
Instantiate(currentWave.GetEnemyPrefab(i), currentWave.GetStartingWaypoint().position, Quaternion.identity,transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
yield return new WaitForSeconds (timeBetweenWaves);
}
It should be something like this
IEnumerator SpawnEnemyWaves()
{
// loop through each wave
foreach (WaveConfigSO wave in waveConfigs)
{
// set current wave to this wave
currentWave = wave;
// loop through each enemy in the current wave and instantiate it
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
{
Instantiate(currentWave.GetEnemyPrefab(i), currentWave.GetStartingWaypoint().position, Quaternion.identity,transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
}
yield return new WaitForSeconds (timeBetweenWaves);
}
Wave 1 should follow the green dots, with current code. Wave 0 follows the yellows dots correctly and wave 1 spawns in the right time frame and follows the correct path pattern but the spawn starts at the centre
That breakdown of the code was super helpful in giving me a better understanding, I’ve tried the code you’ve supplied and deleting the path 1 and readding it and shifting around the start and end points and it’s still not playing. It’s still spawing path 1 from the centre point.
Can I safely delete the prefab for path 1 and retry it?