I’m not sure if I can even call them classes, but okay. I have the variable ‘playtime’ in the file PlayGUI.js.
I need to change that variable from a function in the file Car.js.
How would I go about doing that?
I’m not sure if I can even call them classes, but okay. I have the variable ‘playtime’ in the file PlayGUI.js.
I need to change that variable from a function in the file Car.js.
How would I go about doing that?
If Car.js and PlayGUI.js are both Scripts (i.e. classes that inherit from MonoBehavior), then you can access the variable by setting up a reference to PlayGUI.js in the Start function of Car.js, using GameObject.Find and GetComponent. The following is C#, but you can change it to JS very easily:
MonoBehaviour PlayGUIScript; // Will be "var PlayGUIScript;" in js
// Use this for initialization
void Start () {
PlayGUIScript = GameObject.Find("Name of gameobject to which PlayGUI.js is attached").GetComponent<PlayGUI>();
}
Now the variable is exposed and can be accessed like so:
void Update()
{
// Change the public variable
PlayGUIScript.playtime = whatever new value you give it;
}
It doesn’t have to be in Update, that’s just an example. ![]()
If, contrary to my expectations, those two classes aren’t actually Scripts but custom classes you’ve written, then one class needs a reference to an instance of the other in order to change the value. This is usually passed in the class’s constructor. Let me know if that is the case, and I’ll cook you up a new example.
I get some syntax errors with the GameObject.Find thingy. Unexpected tokens. How would I do that in JS? EDIT: obviously, with regular brackets. It works now, thanks!
– Wolfos