Need a lot of help!

Currently on lecture 118 of the Unity 2D course.

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.


This is a snippet of my general overview


This is what my wave and player prefabs look like


This is my enemy spawner code


And this is my pathfinder code

This is because you spawn all the enemies before you wait. You want to wait inside the for loop, not outside it

image

Your wave config points to one path. There is nothing spawning for the other one. You’ll need a second spawner that points to the other path

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

He had 2 ships spawning at once, I’ll go through the pathfinding section again and see what I might have done wrong. Thanks for the help so far

It’s not the pathfinding. In the EnemySpawner you will eventually have a list of WaveConfigSO which will contain the 2 configs you have.

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.

Finished that lecture and the problem is definitely with path 1.

So the wave configs get dragged in and the results are as follows

Wave 0
Wave 0

Results in ships following path 0 (yellow) as expected

Wave 1
Wave 1

Results in ships spawning from the centre and following path 1 (green)

Wave 0
Wave 1

Results in the same as above

Wave 1
Wave 0

Results in ships following path 0

So, even when I have wave 0/wave 1 set up, it only spawns wave 1 and in the wrong position and doesn’t spawn wave 0 at all?

You will have to share your EnemySpawner, Pathfinder and WaveConfigSO again. But please see this post before you do

Yes, of course. I am getting better at this stuff but not that good yet haha.

Enemy Spawner code

Patherfinder code

WaveConfigSO code

He wants you to use this:
Screenshot 2023-05-03 185543
to paste your code

That way it comes out like actual code. It makes it easier to read and debug.

2 Likes

Please see the post I linked. Screenshots are not good. Sorry, I wasn’t clear

Anyway, here’s the problem
image
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

Thanks.

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

Check the update in my post above. It may help if the code was working correctly

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?

The code that chooses where the enemy spawns looks fine.

image
Click on this game object and send me a screenshot.

Privacy & Terms