0

This is my first time making a 2d game. I'm trying to load a scene if the amount of objects with tag "Enemy" is zero. Line 22 doesn't seem to work. I don't get any errors.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{

    public Animator transition;

    public float transitionTime;

    public GameObject enemyPrefab;
    public GameObject[] enemy;


    // Update is called once per frame
    void Update()
    {
        if (enemy == null)
        {
            enemy = GameObject.FindGameObjectsWithTag("Enemy");
            Debug.Log("null!");
            LoadNextLevel();
        }
    }

    public void LoadNextLevel()
    {
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
    }

    IEnumerator LoadLevel(int levelIndex)
    {
        transition.SetTrigger("Start");

        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(levelIndex);
    }
}
enemy = GameObject.FindGameObjectsWithTag("Enemy");
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Eva
  • 19
  • 4

3 Answers3

1

Initially enemy is null.

So, in the first frame (the first time Update() is called) it satisfies the if condition and then assign something to enemy.

Then, in the second and later frames, enemy is not null anymore, so it does not satisfies if (enemy == null) condition and the lines in the if statement does not be executed. It means that, the lines enemy = GameObject.FindGameObjectsWithTag("Enemy") and LoadNextLevel(); will never be executed after the first frame.

My suggestion is that rewrite the Update() just like this:

void Update() {
    enemy = GameObject.FindGameObjectsWithTag("Enemy"); // get "Enemies" first
    // then do something by using it
    if (enemy == null) {
        Debug.Log("null!");
        LoadNextLevel();
    }
}
Ulas
  • 36
  • 3
1

So, I solved the question. The problem was that the condition was before assigning the variable.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{

    public Animator transition;

    public float transitionTime;

    // Update is called once per frame
    void Update()
    {
        GameObject[] enemy = GameObject.FindGameObjectsWithTag("Enemy");
        if (enemy == null || enemy.Length == 0)
        {
            LoadNextLevel();
        }


        //Debug.Log(GameObject.FindGameObjectsWithTag("Enemy"));
    }


    public void LoadNextLevel()
    {

        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
    }

    IEnumerator LoadLevel(int levelIndex)
    {
        transition.SetTrigger("Start");

        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(levelIndex);
    }
}
Eva
  • 19
  • 4
0

I believe that it's because the variable enemy is of type array and not a list. Arrays are more difficult to deal with and are of a fixed size.

Instead, consider using a List:

List<GameObject> enemy = new List<GameObject>();

Here are some additional resources to assist you with understanding the difference between arrays and lists.

The difference/when to use each

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If the above doesn't work, try `Debug.Log(GameObject.FindGameObjectsWithTag("Enemy");` and see what's printed. If an empty array is printed that means no `GameObject`s with tag eney have been found, and the problem isn't with the code but rather that there's no "Enemy" tagged Game Objects. –  Dec 11 '21 at 15:46