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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Correct syntax for creating a callable string array

Discussion in 'Scripting' started by Larpushka, Aug 12, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QnA : MonoBehaviour {
    5.     public string[,] myarray[] = new string[2, 5]()
    6.     {
    7.         myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    8.         myarray[0, 1] = "bb";
    9.         myarray[0, 2] = "cc";
    10.         myarray[0, 3] = "dd";
    11.         myarray[0, 4] = "ee";
    12.         myarray[1, 0] = "ff";
    13.         myarray[1, 1] = "gg";
    14.         myarray[1, 2] = "hh";
    15.         myarray[1, 3] = "ii";
    16.         myarray[1, 4] = "jj";
    17.         return myarray;
    18.     }
    19.  
    20. }
    21.  
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I think you're looking for this:
    Code (csharp):
    1.  
    2.     public string[,] myarray = new string[,]
    3.     {
    4.         {
    5.             "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    6.             "bb",
    7.             "cc",
    8.             "dd",
    9.             "ee"
    10.         },
    11.         {
    12.             "ff",
    13.             "gg",
    14.             "hh",
    15.             "ii",
    16.             "jj"
    17.         }
    18.     };
    19.  
     
    DonLoquacious likes this.
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  4. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    First off -- thanks a lot! But why can't I simply use this format:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QnA : MonoBehaviour {
    5.     public string[,] myarray = new string[,]
    6.     {
    7.         myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    8.         myarray[0, 1] = "bb";
    9.         myarray[0, 2] = "cc";
    10.         myarray[0, 3] = "dd";
    11.         myarray[0, 4] = "ee";
    12.         myarray[1, 0] = "ff";
    13.         myarray[1, 1] = "gg";
    14.         myarray[1, 2] = "hh";
    15.         myarray[1, 3] = "ii";
    16.         myarray[1, 4] = "jj";
    17.         return myarray;
    18.     }
    19. }
    20.  
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    You're mixing two different ways to create an array. There's creating an empty, fixed-size array and filling it with elements:

    Code (csharp):
    1. string[,] myarray = new string[2,5]
    2. myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    3. myarray[0, 1] = "bb";
    4. myarray[0, 2] = "cc";
    5. myarray[0, 3] = "dd";
    6. myarray[0, 4] = "ee";
    7. myarray[1, 0] = "ff";
    8. myarray[1, 1] = "gg";
    9. myarray[1, 2] = "hh";
    10. myarray[1, 3] = "ii";
    11. 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:

    Code (csharp):
    1. string[,] myarray = new string[,]
    2.     {
    3.         {
    4.             "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    5.             "bb",
    6.             "cc",
    7.             "dd",
    8.             "ee"
    9.         },
    10.         {
    11.             "ff",
    12.             "gg",
    13.             "hh",
    14.             "ii",
    15.             "jj"
    16.         }
    17.     };
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QnA : MonoBehaviour {
    5.     public string[,] myarray = new string[2, 5];
    6.  
    7.     void Awake()  
    8.     {
    9.         myarray[0, 0] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    10.         myarray[0, 1] = "bb";
    11.         myarray[0, 2] = "cc";
    12.         myarray[0, 3] = "dd";
    13.         myarray[0, 4] = "ee";
    14.         myarray[1, 0] = "ff";
    15.         myarray[1, 1] = "gg";
    16.         myarray[1, 2] = "hh";
    17.         myarray[1, 3] = "ii";
    18.         myarray[1, 4] = "jj";
    19.     }
    20. }
    21.  
     
    Last edited: Nov 17, 2015
    roojerry and ThermalFusion like this.
  6. Sersiego

    Sersiego

    Joined:
    Nov 17, 2015
    Posts:
    3
    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[]=newstring[2, 5]();"
    The second pair of "[]" after "myarray" are necessary?
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    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.