Reading a Matrix from a .txt for level buildup

Okay, so I’m fairly new to Unity, js and C#, but I have to create a game for a school project. I’m working on a 2D tiled level, and I had the idea of saving a set of digits in a txt to use for generating the level. For example:

1,0,2,1
5,2,0,1
4,2,3,1

Every number would be a different type of tile, with 0 being no tile (a gap), 1 being grass, 2 being water etc.
On reading, a separate game object would instantiate a tile on the correct position at start (I guess this will be done with loops, but how would I count the amount of rows and columns then?).
The problem: I don’t know how exactly to make it read the file (read some stuff about System.IO but it is rather confusing), plus I’d like to save this grid in a Matrix (var text[y], right?) for future reference in the game (like checking where a character can go and where not).
How do I do this? Thanks in advance.

In this case, there’s not too much point messing around with System.IO. I recommend learning C# over JS, because it’s something that will be, in general, more useful to you (as well as having better .net compatibility).

I recommend storing the information as serialised objects internally in Unity. Unless there is some pressing need to be able to modify the levels outside, there’s not need to trouble yourself.

Now, because Unity does not support serializing either jagged arrays or multidimensional arrays, you’ll need to create your own special classes for this. Start with an enum for tile types:

[System.Serializable]
public enum TileType
{
    Rocks,
    Trees,
    Water,
    Sand
}

Then, create the data-storage types:

[System.Serializable]
public class TileArray
{
    public Tile[] contents;
    public TileArray(int size)
    {
        contents = new Tile;
    }
}

[System.Serializable]
public class TileArrayArray
{
    public TileArray[] xAxis;

    public Tile this[int x, int y]
    {
        get{
            return xAxis[x].contents[y];
        }
        set {
            xAxis[x].contents[y] = value;
        }
    }

    public TileArrayArray(int sizeX, int sizeY)
    {
        xAxis = new TileArray[sizeX];
        for(int i = 0; i < sizeX; ++i)
        {
            xAxis *= new TileArray(sizeY);*

}
}
}
Now, in any of your classes, you can use
public TileArrayArray tiles = new TileArrayArray(10, 10);
to create a 10x10 grid of ‘Tile’ objects, that can be accessed by using
Tile curPoint = tiles[xPos, yPos];
There you go! Serializable multidimensional arrays.

To read your numbers try this :

public void LoadText (string fileName){
		string[][] dataMatrix; 
		string[] result = File.ReadAllLines(fileName);

		dataMatrix = new String[result.Length][];
		for (int i = 0; i < result.Length; i++) {

			string[] entries = result *.Split (',');* 
  •  	for (int j = 0; j < entries.Length; j++) {*
    

_ dataMatrix = entries;_
* }*
* }*

* for (int i = 0; i < result.Length; i++) {*
_ for (int j = 0; j < dataMatrix*.Length; j++) {
Debug.Log (dataMatrix [j]);
}
}
}*_