0

I'm using Unity 5 and mono develop for C# editing. I have one C# file that reads a CSV file and creates a string from the content within the CSV. I made this class a public class with a public string variable because I want to be able to access this string variable in another class. Here's the CSV Reader class:

using UnityEngine;
using System.Collections;
using System.IO;

public class ReadText : MonoBehaviour{
    public TextAsset textFile;     // drop your file here in inspector
    public string text = ""; 

    void Start(){
        string text = textFile.text;  //this is the content as string
        Debug.Log(text);
    }
}

This class works fine. I get the Debug.Log(text) output on the Unity console when I run it, as expected.

Here's the other class that I'm trying to access the ReadText class public text from:

public class CreateLevel : MonoBehaviour {

    void Start () {

        //First, access the text that was read into the ReadText Class (from CSV file)
        //Find the object "Level Designer" that has the ReadText.cs script attached to it
        GameObject designer = GameObject.Find("LevelDesigner");

        //Get a component of the ReadText Class and assign a new object to it
        ReadText CSVText = designer.GetComponent<ReadText>();

        //Now you can access the public text file String
        string levelString = CSVText.text;
        print(levelString);

        //Second, identify each individual text, line by line. As each text symbol is identified
        //place the corresponding sprite on the allocated space in the game scene
        //When the & symbol is encoutered, go to the next line
        //do all this while there are text symbols, once we have the "!" symbol, we're done
        print(text);
        print("We're inside of CreateLevel Class! :C)");

    }
}

The CreateLevel C# file is attached to an empty object in the scene. Unity runs with no issues but I'm not getting the output from the Debug.Log or print(text) command of this class so I'm missing something here. Any help will be great.

  • 1
    You create a new text variable within the start. The "global" text does not get assigned and the local text is destroyed at the end of start. Remove string in front of text inside of start. Also both are done in start, you cannot be sure which will run first. – Everts Mar 05 '16 at 13:58
  • The public "textFile" is assigned a file from inside the Unity Inspector pane. This assigned file is a CSV file. I needed to access this component from the CreateLevel class which I don't think I actually did from the initial version above. I'll write the solution in comment below. – user6020789 Mar 05 '16 at 18:53

1 Answers1

0

Here's the final solution. I think my issue was that the public TextAsset ("textFile" variable) was not being accessed from the CreateLevel class. Credit goes to AwfulMedia tutorial on YouTube: Unity C# Tutorial- Accessing Other Classes (Part 5). I hope this doesn't downgrade me on Stack overflow, just wanted to give credit where due. I took the tutorial and modified for my purposes.

Here's the new ReadText class:

public class ReadText: MonoBehaviour {
   public TextAsset textFile; 
   //text file to be read is assigned from the Unity inspector pane after selecting 
   //the object for which this C# class is attached to

   string Start(){
       string text = textFile.text;  //this is the content as string
       Debug.Log(text);
       return text;
   }
}

And the new CreateLevel Class

public class CreateLevel : MonoBehaviour {
    //variables needed for reading text from the level design CSV file
    private ReadText readTextComponent;
    public GameObject LevelDesigner;
    public string fileString;

    // Use this for initialization
    void Start () {
        readTextComponent = LevelDesigner.GetComponent<ReadText>();
        fileString = readTextComponent.textFile.text;
        //accesses the public textFile declared in the ReadText class. Assigns the textFile 
        //text string to the CreateLevel class fileString variable

    }
}