string[] update in code doesn't update in game

Hello, I had this problem with changing values in a string from a file
First, I entered the array manually, then I wanted to load it from a file, but it stayed the same as it was before.
So I recreated the problem like this:
I created a string and debugged the values.

public string[] a = new string[] { "a", "b", "c" };
...
Debug.Log(string.Join(", ", a));

The output in console looked right.
then i changed the code

public string[] a = new string[] { "a", "b", "d" };

The output in console didn’t differ from the first one (a, b ,c)

Why is this happening?

  • It also happens after building the game and doesn’t change.
  • The cs file is saved correctly, any other changes are visible.

Look at your script instance in the inspector. What’s the value of “a” in the inspector? That value will overwrite any initialized value from the script.

1 Like

It’s because your object has been serialized, and it happens with any public variable that can appear in the inspector.

Basically, once the script compiles, the Inspector sees a new field and fills it in with the default value (a, b, c), and saves the values. (You could at this point go into the inspector and put anything else in these strings and it’ll save them) Now you go back and make some changes, but when the inspector comes to this variable, it’s like “Oh, I already have a value for this”, and loads the saved values of a,b,c - any new defaults are ignored from this point on.

1 Like

Thank you both! @StarManta , @PraetorBlue
I totally forgot about that. It was a little bit confusing as well, as in the start function I was loading the array from a file so the inspector values should be replaced. :eyes:

Evidently there’s a problem with your loading-from-a-file code?

No, it worked. I still had to change it because I was loading with StreamReader and that only worked inside unity editor.
Now it all works just fine as I set it to private string[ ]