Convert txt file contents to variable

I am trying to write a script which reads a number written in a txt file, then reacts a different way depending on what it finds there.

Using these two scripts :

…I have come up with his code.

    import System.IO;
    var fileName = "writetest.txt";
    var number1 : String;
    
    
     
    function Start () {
    var sr = new StreamReader(Application.dataPath + "/" + fileName);
    var fileContents = sr.ReadToEnd();
    sr.Close();
       
    var lines = fileContents.Split("\n"[0]);
    number1 = lines[0];    
      }

    
        function Update() {


            if (number1 == 1){
            {
            Debug.Log("Positive");
            }
            
            
            if (number1 == 0){
            {
            Debug.Log("Negative");
            }
           
         }
       }
     }

The txt file currently has a single 0 in it, so I’m trying to get the debug log to say “Negative”.

However, nothing happens in game.

What have I done wrong?

What error message did you get in the console?

Hint: A string that has the value “0” is not the same thing as the number 0. See http://forum.unity3d.com/threads/58867-Convert-String-to-Int-in-C

Does your value of number1 come up null or zero in the inspector. If its null try

(eg) if((number1==0) || (number1==null)) { }

(Tip) variables shouldn’t end with numbers in its name. Try something like this - (eg) number0A, or number1A. If you have to parse your value in the future you may get some errors.

There is no error message in the console. It simply does not do anything (neither “positive” or “negative” is displayed).

I’m afraid I don’t quite understand your hint.

Are you saying that what is read from the text file is not really a 0, or at least not understood in the same way? Metaphorically, it’s more like the word zero rather than a number?

It is null (empty). I tried your suggestion, but the effect was no different.

if (number1 == "1"){
{
   Debug.Log("Positive");
}

if (number1 == "0"){
{
   Debug.Log("Negative");
}

This.

http://www.dotnetperls.com/int-parse

Hi,
let us say the datas in the File i like:
234; 2938; 5657; 5735; ; 45; 2; and so on…

  1. You should take the StreamReader() like
    content = new StreamReader( Application.dataPath + Filename));

  2. var filedatas = content.ReadToEnd();

  3. SpltDatas = filedatas.Split(“;”[0]);

  4. Convert it to FLOAT or INT
    Float with parseFloat()

Maybe this helps?

Thank you! This solved it.

Here’s the full code with your update, for anyone who has similar problems :

    import System.IO;
    var fileName = "writetest.txt";
    var number1 : String;

    
    
     
    function Start () {
       
    
    var sr = new StreamReader(Application.dataPath + "/" + fileName);
    var fileContents = sr.ReadToEnd();
    sr.Close();
       
    var lines = fileContents.Split("\n"[0]);
    number1 = lines[1];    
      }

    
        function Update() {

    //check reads number of second line
    if (number1 == "1"){
    {
       Debug.Log("Positive");
    }
    }
     
    if (number1 == "0"){
    {
       Debug.Log("Negative");
    }

}
}

If you are treating a string as a number it would be more logical to use parseInt to convert it:
var n = parseInt(number1);
if(n == 1) Debug.Log(“positive”);

You’re going to run across situations where you need the actual value, so it would be better to get used to it.