I’m writing a game in Unity using C#. As part of the game, I’ve got a script attached to a few objects. This script contains the OnGUI() method. As I found out before in my other question, the script and thus OnGUI() gets called multiple times.
Now I’m trying to insert a TextField, enabling the player to enter a value of money he wants to carry with him from the bank (the selected value is given to the player when he presses a button to confirm). But whatever I try to do, the value in the textfield continues to be 0 (starting value).
I’m wondering if there is a way to enable the TextField to be typed in, without it being overwritten by multiple OnGUI() functions from the same script attached to multiple objects, back to 0?
Create a blank object in your Unity Scene, and attach the script to it. Put the contents of the text field into a public variable int, and reference the variable from other scripts. You can use GameObject.Find and GetComponent method of the script to access the variable and write to the variable if necessary. Also if you want, you can do a PlayerPrefs.SetInt or SetFloat and a GetInt, GetFloat to set and retrieve the values. But that is cumbersome on resources.