Laser Defender! problems with WaveSpawner

Hi,
I have some problems with my EnemySpawner. When I start the game it loads just the firs wave then nothing. I’ve debug the code and the only thing that shows is:

Severity Code Description Project File Line Suppression State
Warning CS0649 Field ‘EnemySpawner.waveConfigs’ is never assigned to, and will always have its default value null Assembly-CSharp D:\Unity software\Repos\Unity2D\Laser Defend2D\Laser Defend\Assets\Scripts\EnemySpawner.cs 8 Active

|Warning|CS0649|Field ‘Player.laserPrefab’ is never assigned to, and will always have its default value null|Assembly-CSharp|D:\Unity software\Repos\Unity2D\Laser Defend2D\Laser Defend\Assets\Scripts\Player.cs|10|Active|

|Warning|CS0649|Field ‘WaveConfig.enemyPrefab’ is never assigned to, and will always have its default value null|Assembly-CSharp|D:\Unity software\Repos\Unity2D\Laser Defend2D\Laser Defend\Assets\Scripts\WaveConfig.cs|9|Active|

|Warning|CS0649|Field ‘WaveConfig.pathPrefab’ is never assigned to, and will always have its default value null|Assembly-CSharp|D:\Unity software\Repos\Unity2D\Laser Defend2D\Laser Defend\Assets\Scripts\WaveConfig.cs|10|Active|
.

Also, I have an issue if I use GetMoveSpeed()

But If I use MoveSpeed() everything is ok

This is how the screen looks and I only get the first wave, regardless of the wave number.

Many thanks

Hi Matei,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Those messages with the yellow icons are harmless warnings, probably caused by a bug in Unity. Double click on each message in your Console, and add = default; behind the respective variable declaration.

Regarding the red error message, check if MoveSpeed() is public in the WaveConfig class.

Hope this helps :slight_smile:


See also;

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

Thank you. Unfortunately, the code is fairly difficult to read because it is not formatted. Please format it properly.

Does MonoBehaviour have the same colour as your classname? If not, there might be a problem with Visual Studio. Please watch lecture “Fixing Visual Studio Problems” (currently #4).


See also:

yes they have the same colour.

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());
        }
    }

}
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; }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
    [SerializeField] WaveConfig waveConfig;
    [SerializeField] List<Transform> 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);
        }
    }
}

Add Debug.Logs to your code. For example, check if waveIndex gets increased, and how many times SpawnAllEnemiesInWave gets executed.

Also check the WaveConfig objects. Maybe NumberOfEnemis() returns 0.

OK, thank you I ll try to see what s happening

waveIndex doesn’t increses, I ve rewrite the code , now it’s working fine.
Sorry for all the trouble.
Many thanks:D

Good job! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms