String array basic functionality

So I decided to make a string array to hold a list of the levels that the player has cleared. (This seems like the most reasonable method to me.)

But I can’t seem to get any functions to work with the array. The documentation says that arrays have an “add” function to add new elements to an array, but when I use it, I get a compile error that reads:

And I made a for loop that would run through all of the items in the array to see if a given string was in the array, but when it executes it gives me a null reference error, pointing at the line where I declare the for loop.

public string[] ClearedLevels;

public bool IsLevelCleared(string LevelName) {
            for (int i = 0; i < ClearedLevels.Length; i++) {
                if (ClearedLevels[i] == LevelName)
                    return true;
            }
        return false;
}

What gives? Is there something special about string arrays where they don’t use the same functionality as other arrays?

That function builds here, after defining ClearedLevels[ ]. It looks like the compiler error refers to something else.

Arrays and Lists have completely different functionality. In most statically typed languages , arrays are a runtime constant and therefore can’t be appended ( that’s not quite the case for C# but i digress) . Instead of using an array in this instance you can use a List which is basically a dynamic array ie you can change it’s length at Runtime.

// make sure to include this
using System.Collections.Generic;

class LevelManager : MonoBehaviour
{
    public List<string> CompletedLevels;

    void Start ()
    {
        CompletedLevels = new List<string>();
    }

    public void OnLevelComplete (string level)
    {
        CompletedLevels.Add(level);
    }

    public bool IsLevelCleared (string level)
    {
        return CompletedLevels.Contains(level);
    }


}

It builds fine on my end too, but it still gives me a null reference error when the code is actually executed.
I thought maybe it was a problem with the array technically having nothing in it at all, so I tried feeding it some dummy data, but then I got a compile error with the “add” function.
Is there something wrong with how I defined the array? I added that code into my first post.

I tried adding the list like you showed but I gave me a compile error saying the type or namespace “list” cannot be found.
Likewise when I try to add it into my code, monodevelop only pops up with IList, ArrayList, and SortedList, but no List.

It doesn’t say that. If you’re referring to the JS Array class, that’s not available in C#. (But really shouldn’t be used in JS either, and in any case does not refer to standard arrays.) For language and .NET features, see the MSDN docs.

–Eric

Well great, that explains a few things.
Okay, I’ve looked through the MSDN docs and it looks like I can’t use arrays like I thought, so I guess I’ll have to use a list instead.

But I still can’t get ANYTHING to compile when I try making this a list. The code Tiatang provided doesn’t compile because Unity doesn’t consider “list” to be a real variable. I tried looking up IList but I think that isn’t something that works like what I want, either.

The code looks fine to me and compiles without errors.

–Eric

you are missing the namespace inclusion , you HAVE to add

using System.Collections.Generic;

for Lists to be recognised!

Oh!
Okay, now it is working! Thank you!