Hi, I’m looking to make a simple script that saves some variables to a text file or a xml file. Do I have to do this in C# or could I do this in javascript.
Any simple script to save a variable to a text file out there ?
You can use System.IO equal than in visual studio.
System.IO Namespace | Microsoft Learn
so, you can save a file, xml and so on.
Ok, I will take a look at it.
But does this working with javascript ?
any example of a script ?
Here is a basic script to write text using UnityScript:
iSaveString="your text goes here";
iFileName="c:\somefile.txt";
iStream = System.IO.StreamWriter(iFileName);
iStream.WriteLine(iSaveString);
iStream.Close();
You can take it from there and change the filename where the file will be written to and the saveString (whatever will be written in the file)
Thanks but i get a message that the path contains invalid characters …
Are you on PC or Mac? If you are on Mac, then translate the pathname into the UNIX path that you want to use. If you are on a PC, then just make sure that you use a path that exists on your hard drive.
I’m on pc and the path exist …
should all of the code goes inside the update function ?
I’ve tried all things that I’ve came up with ?
Oh, on PC, you may need to use double slashes… I’m not sure, since I am on a Mac.
iFileName="c:\\somefile.txt";
It worked thanks
I thought I had tried that one
but when I’m going to load the variable from that text file how would I do this ?
something like this I think
whit iStream.ReadLine(iSaveString);
savedData = System.IO.File.ReadAllText("c:\\somefile.txt");
Thanks man
But how do you find those stuff to javascript
I’ve looked on the website but its just for C#
You can use most of the .Net commands in c# or UnityScript (or Boo). At least you can use the commands that are supported by the version of Mono that Unity uses. In general for these type commands, I just look up the syntax in MSDN on microsoft.com and it all just works like you would expect it to. Don’t get hung up on looking for some kind of Javascript specific commands. All three languages are compiled to mono in the end, and they all can do the same stuff.
ok, because I looked on a C# code and the way you get to use System.IO are different and when the don’t have a javascript example I get lost
in C# the do using System.IO
All of it is the same in C# and Javascript (and Boo, for that matter).
–Eric
Ok, thanks guys.
My code almost work now …
just one problem when I want to change my variable from 20 to 30 It says cant convert string to int or something