- Home /
Minor code changes not showing up in preview/run
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?
Unity recompiles all scripts as soon as they are saved, as it "watches" all the files under the Assets folder. You must either not be saving your scripts before compiling, or you have an error in your script(s), which prevents Unity from compiling and saving the current version (it will rollback to the previous working version if there are any script errors).
Answer by StephanK · Jun 12, 2010 at 10:28 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.
Oh, he didn't mention he was trying to edit the variable through code ins$$anonymous$$d of the editor... why are these questions always simpler than they're made out to be? :(
Answer by JonManatee · Jun 12, 2010 at 10:31 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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to update game for happenings while player is away? 0 Answers
Trying to disable the Script below on trigger or the closest thing 0 Answers
Select sprite preview images not showing after upgrading to 2020.2.2f1 3 Answers
Update Preference Item when Preference menu is closed 1 Answer