Enabling and disabling gameObjects using a list

Hello, I’ve been looking on google for a while and I can’t figure out how to clear an array.

Arrays are of a fixed size so clearing it just means filling it with some default value. Unless you mean a List, in which case you can just call Clear() on it.

how?

for (int i = 0; i < myArray.Length; i++) {
  myArray[i] = default;
}

Loop through the array and set each of the value to default/ null

Gives me errors for line 1 6189729--678681--upload_2020-8-11_10-4-1.png

Share your code?

    void ClearAray()
    {
        for (int i = 0; i < gameObjectArray.4; i++){
            gameObjectArray = <some default value>;
    }

Try this:

    void ClearAray()
    {
        for (int i = 0; i < gameObjectArray.Length; i++){
            gameObjectArray[i] = null;
        }
    }

You have to replace “” with an actual value that’s assignable to these elements.

For example, if it’s an array of GameObjects, it must be a value assignable to GameObject, either an actual instance of GameObject or the type’s default, which is ‘null’.

Btw, you can also use built-in methods, such as:

System.Array.Clear

Here’s an example with an integer array:

var array = new int[] { 0, 1, 2, 3, 4, 5 };
System.Array.Clear(array, 0, array.Length);

Or write a generic extension method so that you can call it as if it were a member of an array.

doesn’t work

I mean it certainly sets your whole array to null. Care to elaborate on what isn’t working?

It just doesn’t seem to do that.

How do you know? Are you printing out the contents?

You need to share more information. Unless anyone here has clairvoyance, posts like “doesn’t work” are pretty much useless.

Seeing the whole script would help, along with some information on what you expect the script to do (and what it is incorrectly doing instead)

1 Like

As @bobisgod234 suggests, here is some guidance:

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

Help us to help you.

3 Likes

You’re trying to null the array elements without knowing if the array is of a nullable type. If it’s an array of ints for example, then you can’t null the array elements.

Well I originally told op to use the default keyword which would work no matter what. Then his array was revealed to be named “gameObjectArray” so I made an informed assumption.

2 Likes

I’m going to throw a random guess out here then. When you say you want to “clear” the array, do you mean you want its length to become 0 after removing all references?

If so, arrays don’t have that capability. Once you define an array with a length…

object[] objectArray = new object[10];

…That length cannot be changed. You can make every reference in the array null, but it will always keep its length.

You’d want to use a List, as mentioned above, which can have a dynamic length.
Lists also do have a Clear method, which is probably what you’re looking for. It will remove all references and set is length to 0.

1 Like

I’ve gotten it set up as a list (hopefully) by creating the list in my void Start

    void Start ()
    {
        ArrayList LevelList = new ArrayList();
        rb = GetComponent<Rigidbody>();
        count = 0;
        level = 1;
        PickupCountUpdate ();
        SetCountText ();
        SetLevelText ();
        winText.text = "";
        ClearOtherLevels ();
    }

and adding “Level 2” in the ClearOtherLevels void.

    void ClearOtherLevels ()
    {
        ClearAray();
        if (level == 1)
        {
            LevelList.Add("Level 2");
        }
    }

However unity is telling me that “The name ‘LevelList’ does not exist in the current context” on line 6 of void ClearOtherLevels. Also how would I go about disabling and enabling GameObjects with tags using a list?