Reading .txt file c# problem
Hello I made a Terrain Detail Script with saving the changes to a .txt file. But the problem is the loading doesn't work. Can anyone help me?
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
public class QualityManager : MonoBehaviour {
public float detailMesh;
public float detailTexture;
private string rawText;
private string[] rawArray;
private int intworker;
// Use this for initialization
void Start () {
Debug.Log ("start");
Load();
Terrain.activeTerrain.heightmapPixelError = detailMesh;
Terrain.activeTerrain.basemapDistance = detailTexture;
}
void Load()
{
rawArray = File.ReadAllLines("qualityManager.txt");
foreach(var line in rawArray)
{
if(line.Length < 2 || line.Substring(0, 2) == "//")
continue;
else if(line.Substring(0, 19) == "Terrain Mesh Detail")
{
rawText = line.Substring(22, 30);
int.TryParse(rawText, out intworker);
detailMesh = intworker;
Debug.Log ("read 1");
continue;
}
else if(line.Substring(0, 22) == "Terrain Texture Detail")
{
rawText = line.Substring(25, 30);
int.TryParse(rawText, out intworker);
detailTexture = intworker;
Debug.Log ("read 2");
continue;
}
}
}
}
But the save function does work.
public void Save()
{
rawText = "//Quality System Info save file, version 1 \r\n" +
"Terrain Mesh Detail = " + detailMesh + " \r\n" +
"Terrain Texture Detail = " + detailTexture + " \r\n";
File.WriteAllText("qualityManager.txt", rawText);
}
I have no idea what's wrong, please help me.
"Doesn't work" is not helpful information. What errors are you getting? What is the expected behavior, what is actually happening? Describe your problem in detail.
It doesn't do anything, I expect it to read the variables from the .txt file and use them in the terrain settings so players can change the settings from an options screen. Also there are no errors.
You have log messages in there. Do any of them display? Does it get into the foreach loop at all? Add Debug.Log statements before if
statements and log the values that the if
statements are checking. Log the line itself right after foreach
. Does it contain values you're expecting?
I get only the start debug. The complete loading function doesn't work.
Okay then, you'll need to go through and add the debug messages like I mentioned. Either that or use the debugger to step through the code. Start with one right after foreach.
Debug.Log("Line: " + line);
If it doesn't get there then the file it's trying to read is empty. If it logs something then do the values you see match up with the parsing that you're doing? Do the first 19 characters of any of the lines equal "Terrain $$anonymous$$esh Detail"?
Also, let's see the file that's being written out by Save.
Your answer
Follow this Question
Related Questions
How do I save coordinates to a text file? 2 Answers
Cycle through different txt files to update positions 0 Answers
Save Blendshape values 0 Answers
How to read Excel files in Unity? 5 Answers