Using StreamReader

Hi there. I have a text file with data that I want to read. I want to be able to search through the file until I find a specific piece of data. My code thus far is (aside from terrible):

public int numPlanets;
private string[,] planNameOwnImprov = new string[6,3];

void Start()
{
	LoadFile();
}

void LoadFile()
{
	string line = null;
	StreamReader reader =  new StreamReader("SystemTypeData.txt");
	do
	{
		line = reader.ReadLine();
		if(line == gameObject.name)
		{		
			line = reader.ReadLine();
			numPlanets = int.Parse( line );
			for( int i=0 ; i<numPlanets ; i++ )
			{
				line = reader.ReadLine();
				planNameOwnImprov[i,0] = line;
				planNameOwnImprov[i,1] = "no";
				planNameOwnImprov[i,2] = "poor";
			}
			break;
		}
	}
	while( line!=null )
}

Can anyone offer any help as to what to do. There seems to be nearly nothing useful on the unity forums or answers that I could find. I would just like some generic code to set up the file for I/O. Using C#. Thanks very much!

Let me clarify this a bit. I have used streamreader before many times. I don’t know how to implement it in unity. Where should I be placing my txt files? How do i access the text file from unity?

is there any way to read text file, out side the directory of the project, bcz whenever i try to read a text file outside the project directory it gives null reference (some part of the path is missing).

This is not a Unity question. It’s a programming question, and the obvious answer to me is to Read the Docs:

Stream Class (System.IO) | Microsoft Learn

If that’s too confusing you could try reading this:

C# - File I/O

Then if you still need help you should read the other top 5 google links (where I found these).

In short, do some basic homework before asking questions.

I don’t know how to implement StreamReader in Unity per se but to answer the OP’s question about where to put the text file. You want to define it as

public TextAsset TextFile;

in your script (make sure you have a using UnityEngine; referance) Assign the specific file to whatever Gameobject your script is attached to in the inspector of the editor window by dragging and dropping from the assets folder to where the TextAsset is, in the example code I called it TextFile, so there would be a field labeled "Text File".