x


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?

more ▼

asked Jun 12 '10 at 09:39 PM

Dozer gravatar image

Dozer
46 3 3 7

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).

Jun 12 '10 at 09:45 PM qJake
(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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.

more ▼

answered Jun 12 '10 at 10:31 PM

JonManatee gravatar image

JonManatee
251 7 8 18

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 12 '10 at 10:28 PM

StephanK gravatar image

StephanK
6k 39 53 93

You are exactly right, thank you!

Jun 12 '10 at 10:38 PM Dozer

Oh, he didn't mention he was trying to edit the variable through code instead of the editor... why are these questions always simpler than they're made out to be? :(

Jun 12 '10 at 11:02 PM qJake

Because that would be too easy. ;)

Jun 13 '10 at 04:09 PM StephanK
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x494
x155
x40
x8
x4

asked: Jun 12 '10 at 09:39 PM

Seen: 1107 times

Last Updated: Jun 12 '10 at 09:39 PM