Correct syntax for creating a callable string array

Hi guys, I’m trying to create a string array that I can use in a inheritable class…I get a syntax error.

Assets/Scripts/QnA.cs(5,26): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable’s identifier. To declare a fixed size buffer field, use the fixed keyword before the field type

using UnityEngine;
using System.Collections;

public class QnA : MonoBehaviour {
    public string[,] myarray[] = new string[2, 5]()
    {
        myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        myarray[0, 1] = "bb";
        myarray[0, 2] = "cc";
        myarray[0, 3] = "dd";
        myarray[0, 4] = "ee";
        myarray[1, 0] = "ff";
        myarray[1, 1] = "gg";
        myarray[1, 2] = "hh";
        myarray[1, 3] = "ii";
        myarray[1, 4] = "jj";
        return myarray;
    }

}

I think you’re looking for this:

    public string[,] myarray = new string[,]
    {
        {
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
            "bb",
            "cc",
            "dd",
            "ee"
        },
        {
            "ff",
            "gg",
            "hh",
            "ii",
            "jj"
        }
    };
1 Like

That semicolon on the end is something I always look for first in this kind of thing, right off. I blame C#'s loose enforcement of this rule for Enums (where you can either use it or not) as creating bad habits. Of course, that was only a third of the problem here.

First off – thanks a lot! But why can’t I simply use this format:

using UnityEngine;
using System.Collections;

public class QnA : MonoBehaviour {
    public string[,] myarray = new string[,]
    {
        myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        myarray[0, 1] = "bb";
        myarray[0, 2] = "cc";
        myarray[0, 3] = "dd";
        myarray[0, 4] = "ee";
        myarray[1, 0] = "ff";
        myarray[1, 1] = "gg";
        myarray[1, 2] = "hh";
        myarray[1, 3] = "ii";
        myarray[1, 4] = "jj";
        return myarray;
    }
}

You’re mixing two different ways to create an array. There’s creating an empty, fixed-size array and filling it with elements:

string[,] myarray = new string[2,5]
myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
myarray[0, 1] = "bb";
myarray[0, 2] = "cc";
myarray[0, 3] = "dd";
myarray[0, 4] = "ee";
myarray[1, 0] = "ff";
myarray[1, 1] = "gg";
myarray[1, 2] = "hh";
myarray[1, 3] = "ii";
myarray[1, 4] = "jj";

And there’s the quick, inline creation where you just put in the elements in brackets and call it a day:

string[,] myarray = new string[,]
    {
        {
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
            "bb",
            "cc",
            "dd",
            "ee"
        },
        {
            "ff",
            "gg",
            "hh",
            "ii",
            "jj"
        }
    };

You can’t use the first version when defining a field, as the assignment calls have to be in a method. In the Unity context, the easiest way to do it is to generate the array on Awake:

using UnityEngine;
using System.Collections;

public class QnA : MonoBehaviour {
    public string[,] myarray = new string[2, 5];

    void Awake()  
    {
        myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        myarray[0, 1] = "bb";
        myarray[0, 2] = "cc";
        myarray[0, 3] = "dd";
        myarray[0, 4] = "ee";
        myarray[1, 0] = "ff";
        myarray[1, 1] = "gg";
        myarray[1, 2] = "hh";
        myarray[1, 3] = "ii";
        myarray[1, 4] = "jj";
    }
}
2 Likes

Hello, I am a beginner in Unity and I am also trying to create an array of strings. This threat is very enlighting but the final example is right?:
“publicstring[,] myarray[ ]=newstring2, 5;”
The second pair of “[ ]” after “myarray” are necessary?

Nope, that’s horribly wrong. I’ll update my post.

Thanks for catching that! The post now shows the correct syntax. It must’ve been really late when I wrote that.