Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

String array basic functionality

Discussion in 'Scripting' started by Marscaleb, Feb 21, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    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.

    Code (CSharp):
    1. public string[] ClearedLevels;
    2.  
    3. public bool IsLevelCleared(string LevelName) {
    4.             for (int i = 0; i < ClearedLevels.Length; i++) {
    5.                 if (ClearedLevels[i] == LevelName)
    6.                     return true;
    7.             }
    8.         return false;
    9. }
    What gives? Is there something special about string arrays where they don't use the same functionality as other arrays?
     
    Last edited: Feb 21, 2015
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    That function builds here, after defining ClearedLevels[]. It looks like the compiler error refers to something else.
     
  3. Tiatang

    Tiatang

    Joined:
    Jun 1, 2014
    Posts:
    31
    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.
    Code (csharp):
    1.  
    2. // make sure to include this
    3. using System.Collections.Generic;
    4.  
    5. class LevelManager : MonoBehaviour
    6. {
    7.     public List<string> CompletedLevels;
    8.  
    9.     void Start ()
    10.     {
    11.         CompletedLevels = new List<string>();
    12.     }
    13.  
    14.     public void OnLevelComplete (string level)
    15.     {
    16.         CompletedLevels.Add(level);
    17.     }
    18.  
    19.     public bool IsLevelCleared (string level)
    20.     {
    21.         return CompletedLevels.Contains(level);
    22.     }
    23.  
    24.  
    25. }
    26.  
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    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.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    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
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    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.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The code looks fine to me and compiles without errors.

    --Eric
     
  8. Tiatang

    Tiatang

    Joined:
    Jun 1, 2014
    Posts:
    31
    you are missing the namespace inclusion , you HAVE to add
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    for Lists to be recognised!
     
  9. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Oh!
    Okay, now it is working! Thank you!