Trying to make if(thing == null)

I have added doors into my game, which I want to open once all of the enemies within a room have been killed.

When an enemy is killed, they are destroyed, so I tried the following -

public GameObject[] enemy;
public bool isDone = false;

void Update ()
    {
        if(enemy == null && isDone == false)
        {
            isDone == true;
            //Opening the door()

        }
    }

When an enemy is destroyed, they are classed as missing on the doors inspector. When all of the enemies are missing, I had hoped that == null would be called, but it is not.

Could someone tell me what’s wrong with the script?

Thanks.

The == is a comparison.
Try changing: “isDone == true” to: “isDone = true”.

Yes, one equals too many way used, however I this is a red herring. I will remove that from the below example. The bool of isDone is not relevant, enemy == null is never called.

public GameObject[] enemy;

void Update ()
    {
        if(enemy == null)
        {
            openDoor();
        }
    }

The enemy is within an array list, I feel this is why I am having problems. Once an enemy is destroyed, they are given a missing reference, how do I look for all of the references to be missing? If I could ask ‘if (enemy == missing stuff’, then it would be called.

Yea first you should do isDone = true;
2nd, if you destroy an enemy with Destroy() doesn’t mean that the array will be null at the end. just the element in this array will be null.

public GameObject[] enemy;
public bool isDone= false;

void Update () {
   if(CheckIfAllEnemyDead() && isDone == false) {
      isDone = true;
      //Opening the door()
   }
}

bool CheckIfAllEnemyDead() {
   for ( int i = 0; i < array.Length; i++) {
       if( enemy[i] != null) {
         return false;
      }
   }
   return true:
}

maybe this will work

The enemy array does not become null, just each slot in the array becomes null
the array spaces to hold an enemy are still all there, so not null

you need to check each array element

bool allDead = true;
for(int i = 0; i < enemy.Length; i++){
  if(enemy[i] != null) {
    allDead = false;
    break;
  }
}

Hey, I just worked it out -

    void Update ()
    {
        foreach(GameObject elem in enemies)
        {
            if(elem == null)
            {
                openDoor();
            }
        }

Thanks for the feedback, it is appreciated.

1 Like

your code will openDoor for every enemy which is dead

and that multiple times per second

1 Like

Yeah, the code is -

void Update ()
    {
        if(isDone == false)
        {
            foreach(GameObject elem in enemies)
            {
                if(elem == null)
                {
                    openDoor();
                }
            }
        }
    }

And under openDoor(), I change isDone to true.

more like that yes. but also little hint, don’t use foreach on runtime and updates, causes garbage. better is a for loop.

for(int i = 0; i < enemies.Length; i++) {
      if(enemies[i] == null) {
         openDoor();
      }
 }
1 Like

Thanks @martinmr , I didn’t know that using foreach in Update was a bad idea. I have used your code, but am finding that after each enemy is made null, openDoor() is called. Any idea why?

void Update ()
    {
        if(isDone == false)
        {
            for(int i = 0; i < enemies.Length; i++)
            {
                if(enemies[i] == null)
                {
                    StartCoroutine(openDoor());
                }
            }
        }
    }

Try if (!enemy) instead of if (enemy == null)

1 Like

@LaneFox , just tried it, but it called the function on the first enemy being destroyed. It’s odd.

void Update ()
    {
        if(isDone == false)
        {
            for(int i = 0; i < enemies.Length; i++)
            {
            if(!enemies[i])
            {
                StartCoroutine(openDoor());
            }
            }
        }
    }

Note, I kept the for(int i = etc.) as the console didn’t accept enemies, enemies[ ]. The ! operator cannot be applied to operand of type …GameObject[ ].

I guess keeping for(int i = etc.) is likely the reason why this is not working.

    void Update ()
    {
        if(isDone == false)
        {
            {
            if(!enemies)
            {
                StartCoroutine(openDoor());
            }
            }
        }
    }

Does not work.

Any ideas how to call !enemies when the ! operator cannot be used currently? Thanks.

public GameObject[] enemy;
public bool isDone= false;
void Update () {
   if(isDone == false && CheckIfAllEnemyDead()) {
      isDone = true;
      openDoor()
   }
}
bool CheckIfAllEnemyDead() {
   for ( int i = 0; i < array.Length; i++) {
       if( enemy[i] != null) {
         return false;
      }
   }
   return true:
}

Checks if all enemys are dead, if just one is alive, it will return false so it even hasn’t to go through the rest of the array. If all are dead → null than it will reach the return true.

@kittik yea you are right, my last code posted was opening every time enemy is dead. this should fix it.

1 Like

Thank you @martinmr , that code works as intended, appreciate the assistance.

If I were to keep going with an approach like this I believe I would store my enemies in a List… when they die I’d remove them. Then just check if the List count > 0.

1 Like