Jess
Jess

Reputation: 123

How to take player to the lose scene when timer finishes and does not kill enough enemies

I am creating a game where the player needs to kill a specific number of enemies before the timer runs out. If the player does not kill the number of enemies that he is intended to do so and the timer runs out he will be taken to the lose scene. My problem is that even if the timer runs out and does not kill the required number of enemies he is not taken to the lose scene. How can I resolve this?

EnemySpawner script:

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

public class EnemySpawner : MonoBehaviour {


[SerializeField] GameObject EnemyPreFab;
[SerializeField] int MaxEnemies = 30;
[SerializeField] float EnemySpawnTime = 1.00001f;
[SerializeField] GameObject FirstWaypoint;
int  CurrentNumOfEnemies = 0;
int EnemiesToNextLevel = 7;
int KilledEnemies = 0;
public LevelManager myLevelManager;
public static EnemySpawner Instance = null;
int timesEnemyHit;


IEnumerator SpawningEnemies()
{
    while(CurrentNumOfEnemies <= MaxEnemies)
    {
        GameObject Enemy = Instantiate(EnemyPreFab, this.transform.position, Quaternion.identity);
        CurrentNumOfEnemies++;
        yield return new WaitForSeconds(EnemySpawnTime);
    }
}

void Start()
{
    if (Instance == null)
        Instance = this;
    StartCoroutine(SpawningEnemies());
    timesEnemyHit = 0;
    if (this.gameObject.tag == "EnemyHit")
    {
        CurrentNumOfEnemies++;
    }


}

public void OnEnemyDeath()
{
   /*  CurrentNumOfEnemies--;
     if (CurrentNumOfEnemies < 5)
     {
         // You killed everyone, change scene: 
         LaserLevelManager.LoadLevel("NextLevelMenu");
     }
     */

   /* KilledEnemies++;
    if (KilledEnemies >= EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("NextLevelMenu");
    }
    */

    if(Time.timer == 0.0f && KilledEnemies != EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("Lose");
    }


    }



}

Timer script:

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

public class Timer : MonoBehaviour
 {
public Text TimerText;
public float MainTime;
public static float timer;
public  bool CanCount = true;
public  bool OnlyOnce = false;

void Start()
{
    timer = MainTime; //Timer is gonna be equal to what we set MainTime in the inspector 
}

void Update()
{
    if (timer >= 0.0f && CanCount)
    {
        timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
        TimerText.text = timer.ToString("F");//Converting Timer into a string value 
    }

    //This means that when the timer reaches zero and OnlyOnce is equal to false 
    else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
    {

        CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
        OnlyOnce = true; //This is goning to be done once when it reaches it 
        TimerText.text = "0.00"; //Setting the UI to 0 
        timer = 0.0f; //Setting the timer to 0
    }

    }
}

Upvotes: 0

Views: 72

Answers (2)

Muhammad Farhan Aqeel
Muhammad Farhan Aqeel

Reputation: 717

With your current code structure: Your enemyspawner can have EnemyKilled and EnemiesToNextLevel variables as public variables. And, you can use Instance variable of EnemySpawner inside timer script.

New EnemySpawner.cs

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

public class EnemySpawner : MonoBehaviour {


[SerializeField] GameObject EnemyPreFab;
[SerializeField] int MaxEnemies = 30;
[SerializeField] float EnemySpawnTime = 1.00001f;
[SerializeField] GameObject FirstWaypoint;
int  CurrentNumOfEnemies = 0;
public int EnemiesToNextLevel = 7;
public int KilledEnemies = 0;
public LevelManager myLevelManager;
public static EnemySpawner Instance = null;
int timesEnemyHit;


IEnumerator SpawningEnemies()
{
while(CurrentNumOfEnemies <= MaxEnemies)
{
    GameObject Enemy = Instantiate(EnemyPreFab, this.transform.position, Quaternion.identity);
    CurrentNumOfEnemies++;
    yield return new WaitForSeconds(EnemySpawnTime);
}
}

void Start()
{
if (Instance == null)
    Instance = this;
StartCoroutine(SpawningEnemies());
timesEnemyHit = 0;
if (this.gameObject.tag == "EnemyHit")
{
    CurrentNumOfEnemies++;
}
}

public void OnEnemyDeath()
{
  CurrentNumOfEnemies--;
/*     
if (CurrentNumOfEnemies < 5)
 {
     // You killed everyone, change scene: 
     LaserLevelManager.LoadLevel("NextLevelMenu");
 }
 */
KilledEnemies++;

if (Time.timer > 0 && KilledEnemies >= EnemiesToNextLevel)
{
    LaserLevelManager.LoadLevel("NextLevelMenu");
}   
}
}

Modified Timer script:

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

public class Timer : MonoBehaviour
 {
public Text TimerText;
public float MainTime;
public static float timer;
public  bool CanCount = true;
public  bool OnlyOnce = false;

void Start()
{
timer = MainTime; //Timer is gonna be equal to what we set MainTime in the     inspector 
}

void Update()
{
if (timer >= 0.0f && CanCount)
{
    timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
    TimerText.text = timer.ToString("F");//Converting Timer into a string value 
}

//This means that when the timer reaches zero and OnlyOnce is equal to false 
else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
{

    CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
    OnlyOnce = true; //This is goning to be done once when it reaches it 
    TimerText.text = "0.00"; //Setting the UI to 0 
    timer = 0.0f; //Setting the timer to 0
    if(Time.timer == 0.0f && EnemySpawner.Instance.KilledEnemies != EnemySpawner.Instance.EnemiesToNextLevel)
    {
    LaserLevelManager.LoadLevel("Lose");
    }
        }

    }
}

PS: Code can be improved. Let me know if it helps.

Upvotes: 1

nalnpir
nalnpir

Reputation: 1197

In the update function of the timer

void Update()
{
if (timer >= 0.0f && CanCount)
{
    timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
    TimerText.text = timer.ToString("F");//Converting Timer into a string value 
}

//This means that when the timer reaches zero and OnlyOnce is equal to false 
else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
{

    CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
    OnlyOnce = true; //This is goning to be done once when it reaches it 
    TimerText.text = "0.00"; //Setting the UI to 0 
    timer = 0.0f; //Setting the timer to 0
    EnemySpawner.timeIsZero = true;
}

}
}

then in your EnemySpawner script add a public static boolean timeIsZero = false; on the top and then in the

public void OnEnemyDeath()
 {
   /*  CurrentNumOfEnemies--;
     if (CurrentNumOfEnemies < 5)
     {
         // You killed everyone, change scene: 
         LaserLevelManager.LoadLevel("NextLevelMenu");
     }
     */

   /* KilledEnemies++;
    if (KilledEnemies >= EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("NextLevelMenu");
    }
    */
if(timeIsZero && KilledEnemies != EnemiesToNextLevel)
{
    LaserLevelManager.LoadLevel("Lose");
}


}



}

Upvotes: 0

Related Questions