For loop in public class Game1: game

If i want to make a game with 20 enemy I normaly would make a for loop that would do it for me. But when I want to make a for loop it works perfect in LoadContent, Update, Initialize, ect, but outside where you make the variables a for loop doesn’t work. How can I create x amount of enemys or coins without repeating code over and over?

Hi @Meneer_Ot, Welcome to the Community!

I think what you are trying to do is make a new enemy, have you looked at the samples pages yet?

Samples & Demos | MonoGame Documentation

Here is an advanced option:
c# - XNA: Need help spawning enemies on any side, on a timer tick, then maving them move randomly - Stack Overflow

You could also just limit your spawn count and test against that.

If you could help others help you, post your existing code so we can help you better.

Happy Coding!

You cannot use ‘for’ loops in the declaration space of a class. ‘For’ loops require runtime execution, declaration of variables is done at compile time.

Also, don’t do a variable for each enemy, simply do a

List < Enemy >

or of whatever entity you use as an enemy.


I made a short video about that:

1 Like