x


Bizarre "script changes not changing anything" issue ???

This problem totally explained by Bunny, see below...


Have a variable with a string, like

public var message : String = "some text here";

Arrange to show it on the screen. Hit Play. Now, using your text editor change the text of the variable.

public var message : String = "some text here - edited";

When I hit play again, it simply does not change -- Unity still seems to "know" only about the previous text.

A total mystery to me, any explanation?

bizarre.gif (92.0 kB)
more ▼

asked May 18 '12 at 07:59 AM

Fattie gravatar image

Fattie
19.2k 58 87 148

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

2 answers: sort voted first

That's not a bizarre behaviour... Unity will serialize all public variables of your scripts so you can edit the values in the inspector. If you don't want the variable to be serialized, use the NonSerialized attribute.

copied from comment
You can simply reset your instance by right-clicking at the components header and select Reset. This will erase all serialized data and re initialize the component. If your script has a Reset function, it will be called in this case. Reset is also called for newly added components.

Generally there are 4 types of variables (i've omitted static and protected):

Serialized variables:

public var var1 : String = "text1";  // the public keyword can be omitted in UnityScript

@System.SerializeField
private var var2 : String = "text2";

Non serialized variables:

@System.NonSerialized
public var var3 : String = "text1";  // the public keyword can be omitted in UnityScript

private var var4 : String = "text2";

The confusing thing is maybe that Unity automatically serializes public variables.

Another way is to initialize your variables in Start(). Start is called after the component has been initialized.

public var var5 : String;

function Start()
{
    var5 = "text5";
}

Here're the same examples in C#

Serialized variables:

public string var1 = "text1";

[System.SerializeField]
private string var2 = "text2";  // the private keyword can be omitted in C#

Non serialized variables:

[System.NonSerialized]
public string var1 = "text1";

private string var2 = "text2";  // the private keyword can be omitted in C#


public string var5;

void Start()
{
    var5 = "text5";
}
more ▼

answered May 18 '12 at 10:43 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Ah, of course - I'm so stupid. This does not happen with private variables, of course. Thanks very much for the Reset tip. I always wondered what that did. Ditto for the NonSerialized tip. For future readers: that directive applies just to the one following variable declaration.

@System.NonSerialized
public var message1 :String = "you can 'change' this variable";
public var message2 :String = "you can never 'change' this variable";

Magnificent, thanks again

May 19 '12 at 09:26 AM Fattie
(comments are locked)
10|3000 characters needed characters left

The variable is already defined and Unity will for some reason not save changes to the variable when the variable is edited in script.(I hope that sentence made sense). If you want to change it you cant do it in the script, you would have to do it in the inspector.

more ▼

answered May 18 '12 at 08:31 AM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 112

Yes, I think we all have, but agreed it is a bit weird. But as a general rule you can only change your variables through script once, and that is when you define your variable. Once the variable is defined you can't change the variables content just by editing the variable like you did. If you want to change the variable you will have to change it in the Inspector.

(I'm having some trouble formulating myself today, so sorry about that).

May 18 '12 at 08:45 AM OrangeLightning

You can simply reset your instance by right-clicking at the components header and select Reset. This will erase all serialized data and re initialize the component. If your script has a Reset function, it will be called in this case. Reset is also called for newly added components.

May 19 '12 at 04:27 AM Bunny83

Orange, fortunately thank God Bunny has explained it. this is simply how public variables -- editable in the editor Inspector window -- function. Simply use private variables to avoid this, or, use the Reset button if you need to reset the "default-default value".

May 19 '12 at 09:27 AM Fattie
(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:

x5098
x26
x17
x1

asked: May 18 '12 at 07:59 AM

Seen: 867 times

Last Updated: May 30 '12 at 04:14 PM