Unity 5 returns strange errors when compiling JavaScript code
Hello! Please help! I have successfully made a level with a Terrain, a Rigidbody FPS Controller from Standard Assets -> Characters and some other GameObjects. I needed a saving system - so I have started to google file implementations in JavaScript. I have found some examples - but it returns compiler errors!
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 JavaScript (UnityScript). Code I have used:
#pragma strict
import System;
import System.IO;
var Filename : String = "save.ini";
private var rwfile;
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);}
rwfile = OpenText(Filename);
Debug.Log("File Writer configured sucsessfully");}
function Write() {
rwfile.open("w");
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();}
fixing just this won't get you anywhere because the next problem will come, trust me. just don't piggyback on some code and let others fix it for you. use working code, contact the creator, or write it yourself.
I have wrote it myself, I have just used some functions from examples. Also I didn't find some already working code.
Answer by Kotauskas · Apr 02, 2017 at 12:10 PM
I have already solved this on Stack Overflow, thanks CNuts! Full working code:
#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();}
Your answer
Follow this Question
Related Questions
What's wrong with my script? 2 Answers
Pause Menu Not Going Away 1 Answer
Forge-like in game editor 0 Answers
how do i put a PlayerPrefs into a conditional statement 1 Answer