0
\$\begingroup\$

I'm trying to use the following code to read a text file stored in the Resources folder or in a file on my computer, and instantiate objects in a Unity 3D scene accordingly. I'm having trouble getting it to work. I've tried attaching the script to either the main camera or an empty gameobject. Neither work so far...

I either end up with an error saying the path does not exist (when I put the text file into the project folder on my desktop), or a null exception, or that the path contains invalid characters.

Where am I going wrong?

I'm trying to use a jagged array because I'll need users to eventually be able to change the text file on-the-go to create a level.

The text file contains the following data: S 0 0 0 1 0 0 1 0 2 2 0 0 2 1

My script currently reads:

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using System.Linq;
using System.IO;
using UnityEngine.UI;

public class GenerateLevel : MonoBehaviour {

    public Transform player;
    public Transform ground;
   // public Transform floor_valid;
    public Transform floor_obstacle;
    //public Transform floor_checkpoint;

    //public const string sfloor_valid = "0";
    public const string sfloor_obstacle = "0";
   // public const string sfloor_checkpoint = "2";
    public const string sstart = "S";

    public string textFile = "maze";
     string textContents;

    // Use this for initialization
    void Start () {
        Instantiate(ground);

        TextAsset textAssets = (TextAsset)Resources.Load(textFile);
        textContents = textAssets.text;


        //string[] lines = System.IO.File.ReadAllLines("//maze.txt");

        //string textFile;
        //textFile = Resources.Load("assets/resources/maze").ToString();



        string[][] jagged = ReadFile(textContents);

        for (int y = 0; y < jagged.Length; y++)
        {
            for (int x = 0; x < jagged[0].Length; x++)
            {
                switch (jagged[y][x])
                {
                    case sstart:
                        //Instantiate(floor_valid, new Vector3(x, 0, -y), Quaternion.identity);
                        Instantiate(player, new Vector3(0, 0.5f, 0), Quaternion.identity);
                        break;
                    //case sfloor_valid:
                      //  Instantiate(floor_valid, new Vector3(x, 0, -y), Quaternion.identity);
                        //break;
                    case sfloor_obstacle:
                        Instantiate(floor_obstacle, new Vector3(x, 0, -y), Quaternion.identity);
                        break;
                    //case sfloor_checkpoint:
                      //  Instantiate(floor_checkpoint, new Vector3(x, 0, -y), Quaternion.identity);
                        //break;
                }
            }
        }
    }

    // Update is called once per frame
    void Update () {

    }


    string[][] ReadFile(string file)
    {
        string text = System.IO.File.ReadAllText(file);
        string[] lines = Regex.Split(text, "\r\n");
        int rows = lines.Length;

        string[][] levelBase = new string[rows][];
        for (int i = 0; i < lines.Length; i++)
        {
            string[] stringsOfLine = Regex.Split(lines[i], " ");
            levelBase[i] = stringsOfLine;
        }
        return levelBase;
    }
}
\$\endgroup\$
1
\$\begingroup\$

In your code, you're trying to read two distinct files. Which text file contains the array? The one you're trying to read from Resources or the one you're trying to read with System.IO? Which of these steps is generating the error?

Possible error in Resources.Load:

Based on this line, looks like your Resources folder is called "resources":

//textFile = Resources.Load("assets/resources/maze").ToString();

If this is the case, try to change it's name to "Resources", with a capital R.

Possible error in System.IO:

You have already read one file from Resources and get its content when you call System.IO.File.ReadAllText. This method should receive a path as argument, so unless textContents is a valid path, System.IO.File.ReadAllText won't work properly. So, what is the value of textContents that you read from the first file?

\$\endgroup\$
  • \$\begingroup\$ Thanks. I'd tried changing the capitalisation before and that didn't seem to change anything. Though that might be because there were other errors at the same time. The code I have is an adaptation of someone else's code working a similar problem... in that, they just passed the contents of textContents (i.e. the text file contents as a string) into the ReadAll function. \$\endgroup\$ – user117610 Jul 12 '18 at 18:42
  • \$\begingroup\$ @user117610 This doesn't seem right. What ReadAllText does is to open a file from the path that you pass to it and read its content, as you can see in the docs: link. Probably this is where you're getting the error. If the file in Resources already contains the text that you need, there's no need for System.IO.File.ReadAllText. textContents contains your data already. Try to Debug.Log(textAssets.text) and see what you get. \$\endgroup\$ – Nathalia Soragge Jul 12 '18 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.