Ask coding questions

← Back to all posts
Unity help
Bunnytoes

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)

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

Voters
Bunnytoes
maxina
Comments
hotnewtop
Bunnytoes

@SixBeeps could you please help

MatthewADev

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

Bunnytoes

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

MatthewADev

@Bunnytoes


i think this is the fix:

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

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

}

MatthewADev

@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

@2I9 it comes up with errors

Coder100

oh yeah
makes sense

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

Bunnytoes

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

ANDREWVOSS

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

Bunnytoes

@ANDREWVOSS new error if I do that

Coder100

strange
send code @Bunnytoes

Bunnytoes

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

ANDREWVOSS

@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

@ANDREWVOSS enemyMovement1 script

ANDREWVOSS

@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

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

ANDREWVOSS

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

Coder100

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

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

Bunnytoes

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

ANDREWVOSS

@Bunnytoes You can make everything static, I guess

Coder100

lmao

so
in your wavespawner class

then all you do is WaveSpawner.MinusEnemy();

see

@Bunnytoes

Coder100

@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

@Coder100 True, your solution is better.

Bunnytoes

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

Coder100
Bunnytoes

@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

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

Bunnytoes

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

Coder100

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

Bunnytoes

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

Coder100

just call the function @Bunnytoes

Bunnytoes

@Coder100 I tried it and it doesn't work

Bunnytoes

@Coder100 I fixed the money glitch :)

Coder100

nice!
what was the problem? @Bunnytoes

Bunnytoes

@Coder100 I had to make a boolean

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