- Home /
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.
Why do you need to update the text with the same int every frame? You can just set it once when you read it.
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.
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.
Just declare the prevoof var in the class scope, not inside the function.
Answer by Unitykullanici · Aug 26, 2017 at 09:53 AM
Did you try
function OnApplicationQuit () {
//copy and paste the codes on the Update
}
Your answer
Follow this Question
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