- Home /
Help writing to a file every update/frame (JS)
Hello all,
Here is my code, I wish to save and overwrite an integer variable every frame to a file. (JS)
import System;
import System.IO;
var targetScript: Dupe;
var fileName = "MyFile.txt";
function Start()
{
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
return;
}
var sr = File.CreateText(fileName);
targetScript.oofs -= 0;
}
function Update(){
sr.WriteLine (targetScript.oofs);
r.Close();
}
The error I get is telling me that "sr" is an unknown identifier, probably because the functions are separated and when I try to combine the functions that throws up a missing semicolon error and an unexpected token. I don't know if there is a way that I can get the variable to be identified by using a static variable but I have already tried doing things along those lines and I can't get it to work.
I would appreciate any help very much.
Thanks in advance.
You just need to get sr into class scope, like targetScript and file name. This way Start and Update will be able to reach it.
But I think you mustn't call Close each frame. Btw., what's r?
You have 2 issues there. First, you still use Js which is about to be removed from Unity. Second, you need to look into the concept of scope and variable life. In your case, sr is created in the Start method and won't live outside of it. So when you hit the } of Start method, all the variables created within are lost. So when the update runs, it does not know. You need to make it global by declaring it like filename or targetscript.
@fafase "you still use Js which is about to be removed from Unity" - citation please?
Sure, there have been user speculations for the last 6 months but Unity released an official post lately. It is not for now but slowly co$$anonymous$$g.
https://blogs.unity3d.com/2017/08/11/unityscripts-long-ride-off-into-the-sunset/
On a side note, writing to the file each frame is kinda useless and expensive. Why do you need to do that? Would be better to only save when you need to read. If you need the value elsewhere, it'd be better to connect the two scripts ins$$anonymous$$d.
the "r" was supposed to be "sr", just a typo, fixed now. Yes I have figured it is kind of wasteful. I have looked into the OnApplicationQuit function which is what I really need ins$$anonymous$$d of doing the update, I'm trying to get a save on quit so if the player quits then it will write to a file. I swapped out the Start() for OnApplicationQuit(). However, it doesn't run anymore. Am I missing something? Here is my new code: (thanks for the help)
import System;
import System.IO;
var targetScript: Dupe;
var fileName = "$$anonymous$$yFile.txt";
function OnApplicationQuit()
{
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
return;
}
var sr = File.CreateText(fileName);
targetScript.oofs -= 0;
sr.WriteLine (targetScript.oofs);
sr.Close();
}
EDIT: misplacement of code
I'm thinking your path could be wrong. As it is, it will try and write somewhere in the project folder which may be prevented at runtime (not sure). Can you try to write to the desktop so you can see it easily?
Why do you have to write it every frame, if you do not want to miss anything, just write when it's changed, you'll need an event, that's enough.
Answer by fffMalzbier · Aug 21, 2017 at 08:23 AM
If you define your variable sr of type var in start it's a local variable that is not available outside of the start function so if you try to use it inside of update it says it does not know a variable of name sr.
Define the variable inside the class like you did with "fileName" if you want to make this work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133103im_/https://answers.unity.com/themes/thub/images/avi.jpg)