I have a simple HelloWorld class with the following variable which I pass to GUILayout.Label:
public string message = "Hello World";
If I change the value of the string, it is not reflected when I do a preview or even a full "Build & Run". However, if I change the name of the variable, the modified string will show up from that point on (presumably because a recompilation happened behind the scenes). How can I force this update process to happen without having to trick Unity by renaming a variable?
asked Jun 12 '10 at 09:39 PM
your problem is most likely that you are changing the value of message in the script. Once the script is attached to an object in a scene, all public variables will be accessible from the editor (assuming that you are not using the System.NonSerialized attribute). To change the variable, do so by selecting the GameObject in the Inspector, and modify the variable there. Or remove the script from the GameObject and then reapply it, and the newly added script will read from the new default value you set in the script.
answered Jun 12 '10 at 10:31 PM
If you declare a public variable in a MonoBehaviour this will be exposed to the editor so you can change it directly from the inspector rather than in code. As a consequence of this the default value (in your case "hello world") will only be set once you attach the script to a game component. After that the value of the variable is controlled by the editor. So what you could do is remove the component and readd it. The right way however would be to either not decalare the variable public and change it in code, or (better solution) use the editor to change the value.
answered Jun 12 '10 at 10:28 PM