Edit a script attached to a GameObject in editor mode

In my editor I need to overwrite a script linked to the character. Inside this script there are several variables that it will use after the build.
For brevity we assume there is only the variable of its position.
Via editor I change the position of the character and overwrite the script like this:

string data = "";
data += "using UnityEngine;\r\n\r\n" +
        "public class myData : MonoBehaviour {\r\n" +
        "[HideInInspector] public Vector3 myPosition = newPosition\r\n" +
        "}\r\n";
using (StreamWriter writer = new StreamWriter(getPath, false)) {
    writer.WriteLine(data);
    writer.Flush();
    writer.Close();
}

I remove the script from the character and assign it to it again.
If I look at the script in Visual Studio it is edited correctly.
However, when I start the build, the character remains in the previous position. When viewing the variable in runtime it is in fact the old one and not the new one as per the attached script.
I tried several methods including AssetDatabase.Refresh.
I edit the script via a button in the Inspector. If I repeat the file editing twice, by double clicking on the button, then the position is updated and the character appears in the new position.

If after modifying the script (by clicking the button only once), I go to the script connected to the character and click Reset, then it works straight away.

Do you have suggestions?

Public variables are serialized in the editor. This behaviour is the same as if you edited the script in Visual Studio - the value serialized in the editor would not be overwritten.

Is there a reason you are modifying the script directly instead of just modifying the variable in the editor (by accessing the instance of the script and modifying the variable)?

Since you are hiding the variable in the inspector, and since it appears you don’t want to serialize it, you could make it a property with a public getter and setter instead.

1 Like

Changing the variable in the editor would not be useful to me as the generated script linked to the character will then have to be exported together with other elements to work in another project. Initially I saved the values of the variables in a text file and at runtime I read the file to obtain the settings contained, but this forces me to place this file in a predefined folder. Creating a package with the character with attached scripts would make everything simpler and more automatic.

It seems the trick is to add

[System.NonSerialized] to each variable in the script.
I tested it and it actually works.
Do you think it could cause problems while running the game?

Not if you don’t want to serialise the value in the editor.

You could also make the variables private if you don’t need access from other classes, or as I suggested previously you could make them properties.

public Vector3 MyPosition {get; set;}

In fact, I don’t think I need to serialize these variables, they must exist only for the time the character exists in the play mode or game scene. Furthermore, they must not be saved in the editor with the scene because they are copies of other variables that are fine to be serialized.

I can’t make them private because there are two scripts attached to the character and one of them needs to access these variables in the other script, both in editor mode and after the build.

I’m honest, I don’t fully know the solution to making them properties, but I can certainly delve deeper. Based on what I’m going to use it for, do you think it’s the best solution?

Your solution works too, so I’d say do whatever you’re most comfortable with!