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 24, 2017 at 01:01 PM · staticfunctionspublicfunction updatecalling

Calling Update function in another function (JS)

Hi all,

I'm trying to make a script that makes a text file which stores an int value when the application is exited, then reads it back when re-opened. This all works fine however now I'm trying to get that int variable from the text file to some UI text.

Here is my code:

 import System;
 import System.IO;
 
 var prevooftext : UnityEngine.UI.Text;
 var fileName = "MyFile.txt";
 var targetScript: Dupe;
 
 
 function OnApplicationQuit()
 {
     var sr = File.CreateText(fileName);
     targetScript.oofs -= 0;
     sr.WriteLine (targetScript.oofs);
     sr.Close();
     if (File.Exists(fileName))
     {
         var sa = new StreamReader(fileName);
         var fileContents = sa.ReadToEnd();
         sa.Close();
  
         var prevoof = fileContents;
         print (prevoof);
         Update();
 
      }
  }
 
  function Update(){
  prevooftext.text = prevoof.ToString();
  }

I need to call the Update() function inside of the OnApplicationQuit() function. However obviously the variable can't be used because it's in another function. I need to know how to either call the Update() function inside the OnApplicationQuit() function or know how to use the variable prevoof in both functions.

Sorry if this seems like an obvious answer or if I was not clear.

Thanks for any help in advance.

Comment
Add comment · Show 4
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 26, 2017 at 04:24 AM 0
Share

Why do you need to update the text with the same int every frame? You can just set it once when you read it.

avatar image legowill6 hexagonius · Aug 26, 2017 at 09:25 AM 0
Share

I know, I was just experimenting with different things to see if it would help solve the code even though I don't see how changing a function would.

avatar image RiQQ92 · Aug 26, 2017 at 02:49 PM 0
Share

Do you have to use 'Update()'? You could create other function with parameter you want to pass ins$$anonymous$$d:

  function OnApplicationQuit()
  {
     //....
      if (File.Exists(fileName))
      {
         //....
   
          var prevoof = fileContents;
          print (prevoof);
          OtherFunc(prevoof);
  
       }
   }
  
   function OtherFunc(var param){
         prevooftext.text = param.ToString();
   }

If you must have it in 'Update()' you could define new variable outside of functions after ' var targetScript: Dupe;'. Then assign value to it and call Update() which uses the new variable.

avatar image PizzaPie · Aug 26, 2017 at 06:51 PM 0
Share

Just declare the prevoof var in the class scope, not inside the function.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Unitykullanici · Aug 26, 2017 at 09:53 AM

Did you try

 function OnApplicationQuit () {
    //copy and paste the codes on the Update
 }



Comment
Add comment · Show 1 · 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
avatar image legowill6 · Aug 26, 2017 at 01:42 PM 0
Share

Yeah that was the first thing I tried, no luck.

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

70 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

Related Questions

Can't Encode MD5 2 Answers

Whats the difference between public and static variable? 2 Answers

protected static in Unity CS? 1 Answer

How to access variables from other scripts without static (javascript) 1 Answer

Why is text ui cant be static ? 0 Answers


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