Ask coding questions

← Back to all posts
Unity help
Bunnytoes (111)

I fixed the first error but now I need my enemies to spawn when the enemies left in the wave is 0, here is the code and please help me @Coder100 (sorry for ping)

using System.Collections;
using UnityEngine;
using UnityEngine.UI;


public class WaveSpawner : MonoBehaviour
{
    public static int EnemiesAlive = 0;


    public Wave[] waves;
    public Transform SpawnPoint;
    
    

    public float TimeBetweenWaves = 7f;
    private float countdown = 3f;

    public Text waveCountdownText;


    private int WaveIndex = 0;

  
   

    
    void Update()
    {
        if (EnemiesAlive > 0)
        {
        return;
        }
            

            if (countdown <= 0f && EnemiesAlive == 0)
            {
                StartCoroutine(SpawnWave());

                countdown = TimeBetweenWaves;
            return;
            }

            countdown -= Time.deltaTime;

        countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

        waveCountdownText.text = string.Format("{0:00.00}", countdown);

      




    }

    IEnumerator SpawnWave()
    {
        
        PlayerStats.Rounds++;

        Wave wave = waves[WaveIndex];

        for (int i = 0; i < wave.count; i++)
        {
            SpawnEnemy(wave.enemy);
            yield return new WaitForSeconds(1f / wave.rate);
        }
        WaveIndex++;

        if (WaveIndex == waves.Length)
        {
            Debug.Log("Level Won!");
            this.enabled = false;
        }
    }

    void SpawnEnemy(GameObject enemy)
    {
        
        Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation);
        EnemiesAlive++;
        



    }


}

and screenshots


and

it's supposed to do 1 blue enemy 2 blue enemy 5 blue enemy 3 green enemies and 10 yellow enemies and it doesn't do that

using UnityEngine;

[System.Serializable]
public class Wave
{
public GameObject enemy;
public int count;
public float rate;
}

also if 2+ lasers are hitting one target then the money gets huge

Comments
hotnewtop
MatthewADev (18)

Hello, enemiesalive is a static and not member. change it and then lets see!

Bunnytoes (111)

@2I9 wdym member, please leave a snippet of code for what to change

MatthewADev (18)

@Bunnytoes


i think this is the fix:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour
{
public member int EnemiesAlive = 0;

public Wave[] waves;
public Transform SpawnPoint;



public float TimeBetweenWaves = 7f;
private float countdown = 3f;

public Text waveCountdownText;


private int WaveIndex = 0;





void Update()
{
    if (EnemiesAlive > 0)
    {
    return;
    }
        

        if (countdown <= 0f && EnemiesAlive == 0)
        {
            StartCoroutine(SpawnWave());

            countdown = TimeBetweenWaves;
        return;
        }

        countdown -= Time.deltaTime;

    countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

    waveCountdownText.text = string.Format("{0:00.00}", countdown);

  




}

IEnumerator SpawnWave()
{
    
    PlayerStats.Rounds++;

    Wave wave = waves[WaveIndex];

    for (int i = 0; i < wave.count; i++)
    {
        SpawnEnemy(wave.enemy);
        yield return new WaitForSeconds(1f / wave.rate);
    }
    WaveIndex++;

    if (WaveIndex == waves.Length)
    {
        Debug.Log("Level Won!");
        this.enabled = false;
    }
}

void SpawnEnemy(GameObject enemy)
{
    
    Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation);
    EnemiesAlive++;
    



}

}

MatthewADev (18)

@Bunnytoes and for your money problem, make it detect how much cash was given, if it was too high then do a - format and if its too low then do a + format.

Bunnytoes (111)

@2I9 it comes up with errors

Coder100 (18057)

oh yeah
makes sense

enemiesAlive is a static variable, not a member variable. Very important difference there.

Bunnytoes (111)

@Coder100 could you please leave some code, also putting member instead of static doeesn't work

ANDREWVOSS (187)

@Bunnytoes To make it a member, just remove the static part of the declaration.

Bunnytoes (111)

@Coder100 I mean like leave the edited code in a reply please

using System.Collections;
using UnityEngine;
using UnityEngine.UI;


public class WaveSpawner : MonoBehaviour
{
    public static int EnemiesAlive = 0;


    public Wave[] waves;
    public Transform SpawnPoint;



    public float TimeBetweenWaves = 7f;
    private float countdown = 3f;

    public Text waveCountdownText;


    private int WaveIndex = 0;





    void Update()
    {
        if (EnemiesAlive > 0)
        {
            return;
        }

        


        if (countdown <= 0f && EnemiesAlive == 0)
        {
            StartCoroutine(SpawnWave());

            countdown = TimeBetweenWaves;
            return;
        }

        countdown -= Time.deltaTime;

        countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

        waveCountdownText.text = string.Format("{0:00.00}", countdown);






    }

    IEnumerator SpawnWave()
    {

        PlayerStats.Rounds++;

        Wave wave = waves[WaveIndex];

        for (int i = 0; i < wave.count; i++)
        {
            SpawnEnemy(wave.enemy);
            yield return new WaitForSeconds(1f / wave.rate);
        }
        WaveIndex++;

        if (WaveIndex == waves.Length)
        {
            Debug.Log("Level Won!");
            this.enabled = false;
        }
    }

    void SpawnEnemy(GameObject enemy)
    {

        Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation);
        EnemiesAlive++;




    }


}




using UnityEngine;


[System.Serializable]
public class Wave
{
    public GameObject enemy;
    public int count;
    public float rate;
}
ANDREWVOSS (187)

@Bunnytoes That's wierd. CS0120 happens when you reference a non-static variable from a static method, but none of your methods are static. I'm not sure what's causing that, but it seems like there might be a static reference to EnemiesAlive somewhere else in your project.

Bunnytoes (111)

@ANDREWVOSS enemyMovement1 script

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

[RequireComponent(typeof(EnemyMovement))]
public class enemyMovement1 : MonoBehaviour
{
    private Transform target;
    private int wavepointIndex = 0;

    private EnemyMovement enemy;


    void Start()
    {
        enemy = GetComponent<EnemyMovement>();
        
        target = Waypoints.points[0];
    }
    void Update()
    {
        Vector3 dir = target.position - transform.position;
        transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);

        if (Vector3.Distance(transform.position, target.position) <= 0.4f)
        {
            GetNextWaypoint();
        }
        enemy.speed = enemy.startSpeed;
    }

    void GetNextWaypoint()
    {
        if (wavepointIndex >= Waypoints.points.Length - 1)
        {

            EndPath();
            return;
        }

        wavepointIndex++;
        target = Waypoints.points[wavepointIndex];
    }

    void EndPath()
    {
        PlayerStats.Lives--;
        WaveSpawner.EnemiesAlive--;
        Destroy(gameObject);
    }

}
ANDREWVOSS (187)

@Bunnytoes You can't do WaveSpawner.EnemiesAlive--, because it's a static reference. You'll have to either make a reference to a WaveSpawner object, or make everything in WaveSpawner static.

Bunnytoes (111)

@ANDREWVOSS should I do -= then or what? please tell me

ANDREWVOSS (187)

@Bunnytoes no, you just can't modify WaveSpawner.EnemiesAlive.

Coder100 (18057)

oh i see

yeah just make a method in WaveSpawner called killEnemy that subtracts it there

this isn't js, you can't just change anything. @Bunnytoes

Bunnytoes (111)

@Coder100 please leave the actual code, I know I am annoying rn but this is really bugging me (get it, bad joke)

Bunnytoes (111)

@ANDREWVOSS I have to do that though, is there a work-around?

ANDREWVOSS (187)

@Bunnytoes You can make everything static, I guess

Coder100 (18057)

lmao

so
in your wavespawner class

void MinusEnemy() {
  // ... add the code to subtract one to enemiesalive ...
}

then all you do is WaveSpawner.MinusEnemy();

see

@Bunnytoes

Coder100 (18057)

@ANDREWVOSS what about my method solution?
I use it all the time and I think it's the pattern you are supposed to follow

ANDREWVOSS (187)

@Coder100 True, your solution is better.

Bunnytoes (111)

@Coder100 wait so if I did I would have to call the methods where the EnemiesAlive go down

Bunnytoes (111)

@Coder100 still doesn't work same error I sent @ANDREWVOSS, I created a public void MinusEnemy and the code was EnemiesAlive -= 1;
and I put it into the enemymovement1 and EnemyMovement script also randomly the money goes to a huge number

Coder100 (18057)

you put that in the correct class right
in the WaveSpawner not anywhere else @Bunnytoes

Bunnytoes (111)

@Coder100 I put in into the other scripts where the the enemies alive would have to go down, was I not supposed to?

Coder100 (18057)

only in the wavespawner class
and don't forget to call it @Bunnytoes

Bunnytoes (111)

@Coder100 so I put it in the wavespawner, how do I get it to decrease when the enemy dies and stuff

Bunnytoes (111)

@Coder100 I tried it and it doesn't work

Bunnytoes (111)

@Coder100 I fixed the money glitch :)

Coder100 (18057)

nice!
what was the problem? @Bunnytoes

Bunnytoes (111)

@Coder100 I had to make a boolean

private bool isDead = false;

I have almost finished my td game but I still have the spawning issue, if I fix that it is finished