Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by legowill6 · Aug 19, 2017 at 04:30 PM · javascriptfileupdate functionwrite data

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Aug 20, 2017 at 10:03 AM 0
Share

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?

avatar image fafase · Aug 20, 2017 at 11:29 AM 0
Share

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.

avatar image tanoshimi fafase · Aug 20, 2017 at 12:00 PM 0
Share

@fafase "you still use Js which is about to be removed from Unity" - citation please?

avatar image fafase tanoshimi · Aug 20, 2017 at 06:13 PM 0
Share

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/

avatar image fafase · Aug 20, 2017 at 11:31 AM 0
Share

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.

avatar image legowill6 fafase · Aug 20, 2017 at 11:49 AM 0
Share

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

avatar image fafase legowill6 · Aug 21, 2017 at 07:06 AM 0
Share

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?

avatar image haruna9x · Aug 21, 2017 at 07:15 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Unity not properly flushing file output? 0 Answers

Console.WriteLine Redirect to file 0 Answers

Create image-file from resources image 1 Answer

Is there a way to encrypt/lock a File.CreateText file? (its my highscore) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges