|
This problem totally explained by Bunny, see below... Have a variable with a string, like Arrange to show it on the screen. Hit Play. Now, using your text editor change the text of the variable. 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?
(comments are locked)
|
|
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 Generally there are 4 types of variables (i've omitted static and protected): Serialized variables: Non serialized variables: 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. Here're the same examples in C# Serialized variables: Non serialized variables: 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. Magnificent, thanks again
May 19 '12 at 09:26 AM
Fattie
(comments are locked)
|
|
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. 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)
|
