Array is now filled with 0's

In my tile script I have the following line: “int[,] map = new int[20,20];”. In Start(); I do the following:

int[,] map =         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                      {1,1,3,3,3,3,3,3,3,3,1,1,3,3,3,3,3,2,2,1},
                      {1,1,1,1,1,1,3,3,3,3,1,1,1,3,3,3,3,2,2,1},
                      {1,3,1,1,1,1,3,3,3,3,1,1,1,1,3,3,2,2,3,1},
                      {1,3,3,1,1,1,3,3,3,3,3,3,3,3,3,3,2,2,3,1},
                      {1,3,3,3,1,1,3,3,3,3,3,3,3,3,3,2,2,3,3,1},
                      {1,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,3,3,3,1},
                      {1,3,3,3,1,1,3,3,3,3,3,3,3,2,2,3,3,3,3,1},
                      {1,3,3,1,1,1,3,3,3,3,3,3,2,2,3,3,3,3,3,1},
                      {1,3,3,1,1,1,3,3,3,3,3,3,2,2,3,3,1,1,3,1},
                      {1,1,1,1,1,1,3,3,3,3,3,3,2,2,3,3,1,1,3,1},
                      {1,1,1,1,1,3,3,3,3,3,3,3,2,2,3,3,1,1,3,1},
                      {1,3,3,3,3,3,3,3,3,3,3,3,2,2,3,3,1,1,3,1},
                      {1,3,3,3,3,3,2,2,2,4,2,2,2,3,3,1,1,1,1,1},
                      {1,3,3,3,3,2,2,2,2,4,2,2,3,3,3,1,1,1,1,1},
                      {1,3,3,3,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,1},
                      {1,3,3,2,2,3,3,3,3,3,1,1,1,1,1,3,3,3,3,1},
                      {1,3,2,2,3,3,3,3,3,3,1,1,1,1,1,3,3,3,3,1},
                      {1,2,2,3,3,3,3,3,3,3,1,1,3,3,3,3,3,3,3,1},
                      {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

At the bottom of my script I have a function

    public int GetTileAt(int x, int y)
    {
        int value = map[y,x];

        return value;
    }

When I access this function from a different script the return value is 0. If I do the follow from within the function

foreach(int tile in map)
{
      Debug.Log(tile);
}

The Debug.Log says 400 0’s. Why is this? Is it a scope issue?

Yes its a scope issue. Youre typing map as in[,] in start, which makes it local.

You should have a class member int[,] map and then just be assigning it in start()

EDIT: Actually, just take that declaration out of start entirely. Its an array of literals, it doesnt need to reference any components or other run time elements, so you can just declare it as such in the class.

Thanks for the solution. I did as you said and now I’m able to access the array!