Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to create an enemy if the counter reaches 10? (but just one enemy)

Discussion in 'Scripting' started by GabeGabe, Feb 4, 2015.

  1. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    Hi!

    I would like to do a game where there is a counter what just counts up and if this counter >= 10, I would like to instantiate an enemy (prefab) but just one enemy.

    The if state is in the update method so it will instantiate continuously because then the if state is always true.

    And then if the counter >= 20 then Instantiate 2 another enemies and so on...

    How to do this?
     
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    well if you want it to spawn for each 10th enemy then you could just check if counter % 10 is 0 like this:
    Code (CSharp):
    1. if(counter > 0 && counter % 10 == 0)
    2. {
    3.      //Spawn 1 or more enemies (this will be true for every 10th increase of counter - 10, 20, 30 ... ect)
    4. }
     
  3. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    Yeah, I know it, btw thanks. But what I would like to do is to spawn exactly 1 enemy if it reaches 10.
    Because this will spawn enemy 10, 20, 30, 40....etc as you said
     
  4. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Code (csharp):
    1.  
    2. if (counter > 0 & counter % 10 == 0) {
    3.  for (int i=0; i<counter/10; i++) {
    4.    //spawn
    5.  }
    6. }
    7.  
     
  5. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    Thanks, but this is not what I would like to do. What I would like to do is to spawn exactly 1 enemy if it reaches 10.
    You just misunderstood me.
    Sorry
     
  6. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Well then it is simply:
    Code (CSharp):
    1. if(counter == 10)
    2. {
    3.    // spawn
    4. }
    Think this sentence created the confusion:
     
    Last edited: Feb 4, 2015
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'm going to suggest you take some scripting tutorials!

    Very simple things like this wouldn't come up if you had the foundational skills to go along with them. Half the fun of programming is figuring out what problems are and how to solve them.
     
    Ramsdal likes this.
  8. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    I thought of this but I use count += Time.deltaTime and that a float. And it's never == 10.
     
  9. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    You haven't put very much thought into this problem at all. As I said, go back and do some tutorials.

    There are many many ways to solve this problem. Quick Example:
    Code (CSharp):
    1.  
    2. public class EnemySpawner : MonoBehaviour {
    3.  
    4.     GameObject enemyPrefab;
    5.     public int spawnAtCount = 10;
    6.  
    7.     void Update() {
    8.  
    9.         SpawnEnemy();
    10.  
    11.     }
    12.  
    13.     void SpawnEnemy() {
    14.    
    15.         //Get a whole number from the running time. Casting floats to integers simply discounts
    16.         //the decimal, which is what we want.
    17.         int counter = (int)Time.time;
    18.    
    19.         //Modulus operator % returns the remainder from a division operation.
    20.         //if the count is a multiple of spawnAtCount, it will return 0, which is what we want  
    21.         if (count == spawnAtCount)
    22.             Instantiate(enemyPrefab);
    23.    
    24.     }
    25.  
    26. }
    27.  
    Lets say, according to your first post, you want to increase the number of enemies spawned every time the counter is a multiple of 10:

    Code (CSharp):
    1. public class EnemySpawner : MonoBehaviour {
    2.  
    3.     GameObject enemyPrefab;
    4.     public int spawnIncrement = 10;
    5.  
    6.     void Update() {
    7.  
    8.         SpawnEnemy();
    9.  
    10.     }
    11.  
    12.     void SpawnEnemy() {
    13.    
    14.         //Get a whole number from the running time. Casting floats to integers simply discounts
    15.         //the decimal, which is what we want.
    16.         int counter = (int)Time.time;
    17.    
    18.         //Modulus operator % returns the remainder from a division operation.
    19.         //if the count is a multiple of spawnIncrement, it will return 0, which is what we want
    20.         if (count % spawnIncrement == 0) {
    21.    
    22.             //Quite simply, in this example, we want to spawn an additional enemy every
    23.             //time we encounter the spawnIncrement. If we've gotten here, we know the division
    24.             //is going to divide nicely. 10/10 = 1 enemy. 20/10 = 2 enemies. If you
    25.             //changed the spawn increment, it would still work. 7/7 = 1 enemy. 14/7 = 2 enemy. And so on.
    26.             int numEnemies = count / spawnIncrement;
    27.        
    28.             for(var i = 0; i < numEnemies; i ++)
    29.                 Instantiate(enemyPrefab);
    30.        
    31.         }
    32.    
    33.     }
    34.  
    35. }
    Anyway, like I said. Go back, learn some programming basics. It's a lot of fun!
     
  10. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    You could use a bool
    bool spawn=false

    if counter>10 &&spawn==false
    {instantiate ....spawn=true}