-1

I have successfully made a level with a Terrain, a Rigidbody FPS Controller from Standard Assets -> Characters and some other GameObjects. I now need to save my system so I have started to google file IO for UnityScript. I have found some examples but they return compiler errors (details below).

OS is Windows 10 64-bit. Unity version is 5.5.1f1 64-bit. Code editor is Visual Studio 2015, line endings corrected to Windows line endings. Language is UnityScript. Code I have used:

#pragma strict

import System;
import System.IO;

var Filename : String = "save.ini";
private var rwfile : StreamWriter;
var x;
var y;
var z;
var Player : GameObject;

private var startTime : int;
private var timer1 : int;

function TimerSet() {startTime = Time.time;}

function TimerUpdate() {
    timer1 = Time.time;
    if(timer1 > 60) {
        Debug.Log("Saving");
        Write();
        TimerSet();}}

function WriterSetup() {
    Debug.Log("File Writer setup...");
    if (!File.Exists(Filename)) {
        rwfile = File.CreateText(Filename);}
    Debug.Log("File Writer configured sucsessfully");}

function Write() {
    rwfile = File.CreateText(Filename);
    x = Player.transform.position.x;
    y = Player.transform.position.y;
    z = Player.transform.position.z;
    rwfile.WriteLine(x);
    rwfile.WriteLine(y);
    rwfile.WriteLine(z);
    rwfile.Close();}

function Start() {Debug.Log("Saver Script Setup");
    WriterSetup();
    TimerSet();}

function Update() {TimerUpdate();}

My error:

Error

Kotauskas
  • 1,239
  • 11
  • 31

1 Answers1

1

Your error is telling you that it dosen't know what OpenText is. I don't see in your code anything about a function called OpenText, so i guess you were trying to use the File.OpenText function so you should change this line

rwfile = OpenText(Filename);

To this

rwfile = File.OpenText(Filename);

Also for the other errors you need to assigne your rwfile to a StreamWriter

private var rwfile : StreamWriter;

You should also change your Write function for this:

function Write() {
    x = Player.transform.position.x;
    y = Player.transform.position.y;
    z = Player.transform.position.z;

    using (rwfile = File.CreateText(Filename))
    {
        rwfile.WriteLine(x);
        rwfile.WriteLine(y);
        rwfile.WriteLine(z);
        rwfile.Close();
    }
}
I.B
  • 2,925
  • 1
  • 9
  • 22
  • Also it says `WriteLine is not a member of 'Object'` – Kotauskas Mar 31 '17 at 13:25
  • @VladislavToncharov Try changing `private var rwfile;` to `private StreamWriter rwfile;` – I.B Mar 31 '17 at 13:31
  • It says `Assets/SaveScripts/Saver.js(7,9): BCE0043: Unexpected token: StreamWriter.` However, using StreamWriter token removes all compiling errors I had *and raises* a new one. – Kotauskas Mar 31 '17 at 13:41
  • @VladislavToncharov I'm not too good with UnityScript but I think it should be `private var rwfile : StreamWriter;` could you try that, sorry for the confusion. – I.B Mar 31 '17 at 13:49
  • After replacing `private var rwfile;` with `private var rwfile : StreamWriter;` Unity started to understand what I am doing :) *However,* 2 new errors are raised: `Assets/SaveScripts/Saver.js(29,27): BCE0022: Cannot convert 'System.IO.StreamReader' to 'System.IO.StreamWriter'.` and `Assets/SaveScripts/Saver.js(33,12): BCE0019: 'Open' is not a member of 'System.IO.StreamWriter'.` – Kotauskas Mar 31 '17 at 13:56
  • Replacing `rwfile.Open()` with `rwfile = File.OpenText(Filename)` have fixed the second error. But, `Assets/SaveScripts/Saver.js(29,27): BCE0022: Cannot convert 'System.IO.StreamReader' to 'System.IO.StreamWriter'.` remains. – Kotauskas Mar 31 '17 at 14:02
  • @VladislavToncharov You dont really need the line `rwfile = OpenText(Filename);` in `WriterSetup` function. That's why you get that error. – I.B Mar 31 '17 at 14:11
  • Stack Overflow says: `Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?` Also, I have already removed `rwfile = OpenText(Filename);` in `WriterSetup`.. – Kotauskas Mar 31 '17 at 14:17
  • @VladislavToncharov Could you tell me what is on the line 29 where you get your error and also update your question so i can see how your code looks like. – I.B Mar 31 '17 at 14:22
  • I have edited the code in the question so now you can see how the updated code looks like. – Kotauskas Mar 31 '17 at 14:34
  • @VladislavToncharov Do you want to add text to the file each time or simply overwrite everything each time? – I.B Mar 31 '17 at 14:54
  • I want to always overwrite the file because it stores the player's transform. This script saves the player's position every minute (then I will add a manual save button). I also have a "Loader" script, it loads the stored data when the game is launched and moves the player into transform which was fetched from that file. I have no problems with the Loader. This would mean I should _tell_ Unity that I want to write to the file, not read from it. – Kotauskas Mar 31 '17 at 15:00
  • @VladislavToncharov I edited my answer can you try that? – I.B Mar 31 '17 at 15:02
  • @VladislavToncharov what line and what is the error? – I.B Mar 31 '17 at 15:06
  • Line 32 (`rwfile = File.OpenText(Filename); ` in the Write function) raises `Assets/SaveScripts/Saver.js(32,27): BCE0022: Cannot convert 'System.IO.StreamReader' to 'System.IO.StreamWriter'.` – Kotauskas Mar 31 '17 at 15:09
  • @VladislavToncharov what is written on line 32 i can't really know. – I.B Mar 31 '17 at 15:10
  • `rwfile = File.OpenText(Filename);`, first line in the Write function is line 32 – Kotauskas Mar 31 '17 at 15:16
  • @VladislavToncharov As i said use the `Write` function in my answer i removed that line. – I.B Mar 31 '17 at 15:17
  • I have edited the code, see ^^^. The problem is now fixed. Thank you! – Kotauskas Mar 31 '17 at 15:32