Need a lot of help!

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.

Can you describe the behavior more, what happens when you run the program? Does the enemy that spawns in the middle jump to the first waypoint after or straight to the second one?

No, it spawns in the centre and then continues towards the bottom left in the same path of the green waypoints, the second clone spawns in the centre and follows that path. So the only thing that is wrong is where the ships are spawning on path 1

Everything I can see here looks ok so I have to ask some more questions: Are you spawning one type of enemy in wave 0 and another in wave 1? If so, are the enemies that’s supposed to follow path 1 centered in their prefab?

Privacy & Terms