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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why doesn't it progress from one InvokeRepeating function to the next when the "if"condition is met?

Discussion in 'Scripting' started by UNTL1, Apr 2, 2020.

  1. UNTL1

    UNTL1

    Joined:
    Mar 30, 2020
    Posts:
    71
    I'm trying to program a game where the enemies fall from above at higher speeds when a certain amount of kills is achieved. Here is a part of the code where I thought it would progress between speed levels, but after killing 25 enemies it just stops instantiating them:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnemyController : MonoBehaviour
    7. {
    8.     private Transform enemyHolder;
    9.     public float speed;
    10.     public GameObject shot;
    11.     public GameObject enemy;
    12.     public Text winText;
    13.     int secCount;
    14.     float timer = 0;
    15.     public float fireRate = 0.997f;
    16.     public int enemyCount;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         enemyCount = 0;
    21.         secCount = 0;
    22.         enemyHolder = GetComponent<Transform>();
    23.         winText.enabled = false;
    24.         InvokeRepeating("MoveEnemy", 0f, 0.016f);
    25.     }
    26.     private List<GameObject> allSpawns = new List<GameObject>();
    27.  
    28.     void MoveEnemy()
    29.     {
    30.         float xPosition = Random.Range(-11, 11);
    31.         secCount = Random.Range(2, 4);
    32.         timer += Time.deltaTime;
    33.         if (timer >= secCount && enemyCount < 25)
    34.         {
    35.             GameObject spawned = Instantiate(enemy, new Vector3(xPosition, 6, 0), Quaternion.identity);
    36.             allSpawns.Add(spawned);
    37.             enemyCount++;
    38.             timer = timer - secCount;
    39.         }
    40.         foreach (GameObject thisEnemy in allSpawns)
    41.         {
    42.             if (thisEnemy !=null)
    43.             {
    44.                 thisEnemy.transform.position += new Vector3(0, -1 * speed * Time.deltaTime, 0);
    45.             }
    46.         }
    47.         if (PlayerScore.playerScore == 25)
    48.         {
    49.             timer = 0;
    50.             CancelInvoke();
    51.             InvokeRepeating("MoveEnemy2", 0f, 0.016f);
    52.         }
    53.     }
    54.  
    55.     void MoveEnemey2()
    56.     {
    57.         float xPosition = Random.Range(-11, 11);
    58.         secCount = Random.Range(2, 4);
    59.         timer += Time.deltaTime;
    60.         if (timer >= secCount && enemyCount < 50)
    61.         {
    62.             GameObject spawned = Instantiate(enemy, new Vector3(xPosition, 6, 0), Quaternion.identity);
    63.             allSpawns.Add(spawned);
    64.             enemyCount++;
    65.             timer = timer - secCount;
    66.         }
    67.         foreach (GameObject thisEnemy in allSpawns)
    68.         {
    69.             if (thisEnemy != null)
    70.             {
    71.                 thisEnemy.transform.position += new Vector3(0, -1.1892f * speed * Time.deltaTime, 0);
    72.             }
    73.         }
    74.         if (PlayerScore.playerScore == 50)
    75.         {
    76.             CancelInvoke();
    77.             InvokeRepeating("MoveEnemy3", 0f, 0.016f);
    78.         }
    79.     }
    The PlayerScore class seems to be keeping count of the score fine.
    Thanks in advance.
     
  2. kasztelan

    kasztelan

    Joined:
    Nov 3, 2014
    Posts:
    11
    You have a typo:

    MoveEnemey2
     
  3. UNTL1

    UNTL1

    Joined:
    Mar 30, 2020
    Posts:
    71
    Whoops. Lol, thanks.