Search Unity

write to txt?/string to int

Discussion in 'Scripting' started by half_voxel, Mar 31, 2008.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi.
    I wonder if you can write to a .txt file or create a new .txt file and write to that.

    And i also wonder how you can use textfield in GUI and then process it is an int eventually make a script that just takes the numbers.
     
  2. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Hi,

    Because Unity is using Mono, you can use the IO API of Mono
    with adding at the beginning of your C# code :

    using System.IO;
    and the Mono documentation :

    http://www.go-mono.com/docs/index.aspx?link=M:System.IO.TextWriter.Write(System.Decimal)
    and for example

    Code (csharp):
    1.  
    2.  
    3.        TextWriter objWriter=new StreamWriter("C:\\test.txt");
    4.     objWriter.WriteLine("Hello World");
    5.     objWriter.WriteLine("Hello World");
    6.     objWriter.Flush();
    7.  
    8.     objWriter.Close();
    9.     objWriter=null;
    10.  
    Of course, it could depend on the location of the target file if you have read/write access to the directory.
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    can it be done in java (i am not so good at c#)
     
  4. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    of some reson i get an error when writing this:

    Code (csharp):
    1.  
    2.     void Start () {
    3.         public string label;
    4.     }
    5.  
    Expecting ";"
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    oh and i have solved it with writing it in c#