Help me understand the code

HI!
I have a code:

[SerializeField] public GameObject[] objectToDestroy = new GameObject[3]; // 3 Buttons (This my Category)

[SerializeField] public GameObject nextLevelButton; // button change level, Scene
public void DestroyGameObject1() //onclick first button Destroy this Button
    {
        Destroy(objectToDestroy[0]);
    }

    public void DestroyGameObject2() //onclick second button Destroy this Button
    {
        Destroy(objectToDestroy[1]);
    }

    public void DestroyGameObject3() //onclick third button Destroy this Button
    {
        Destroy(objectToDestroy[2]);
    }

I want how many Button are left in objectToDestroy array? If all buttons destroy - start code

I made code:

if (objectToDestroy.Length < 1) //No Error, no count
        {
            print("All Buttons Destroy");
        }

Where error?

9556033--1350793--buttons.jpg

9556033--1350802--category1 destroy.jpg
9556033--1350799--category2 destroy.jpg
9556033--1350796--category3 destroy all category destroy.jpg

Can you explain further? I don’t understand the question.

Hi!
I want count how many button Destroy and if all button Destroy print in console “All button Destroy”

Thank`s

You might want a list of gameobjects rather than an array since your array’s length will never change even if you destroy the gameobjects within it. The cells will then just contain null I think.

First make sure you put this in your code, I’m pretty sure you got it though.

using System;

Now this is the problem:
A null value IE a gameObject that doesn’t exist still counts as part of the length. Think of arrays as storage containers and empty storage counts towards the length.
To fix this make an integer which starts at 0 then adds by 1 each time a button gets destroyed. When it equals the length then print the stuff.

1 Like

Thanks
Good Work!