Updating code at runtime, without loosing all initialized variables

Hi there I know it is possible to update code at runtime in Unity3d.
I run the game, change the code, save the script and the game updates itself.

- However my question is: Is there is option or way how to do this without loosing all my saved variables? Thus breaking entire game after saving your code in runtime.

Let`s say you initialize your variable when game starts, and load a texture to that variable.
Now you run the game, go to change your code (game is still running), make changes, save the code, but when game (recompiles) at runtime, it resets all the variables. Thus your saved texture will reset itself, causing critical errors and null references.

Pretty simple, just check if your texture is null, and if it is, then reassign it

Texture2D texture;

void Update () {
 
 if(texture == null) {
   texture = LoadTexture();
 }

}
Texture2D LoadTexture () {
 //set your texture here
}

You can drag and drop items into exposed variables within the inspector, and they will stay persistent on play as long as they are part of the same prefab.

You can try copying the component during play, and pasting it after you stop the scene again.

If the object is something complex, I suggest exporting the data to a file, and loading it again on playback.

Otherwise ‘load a texture’ is quite vague, I assume you mean drag and drop. In which case, take my first suggestion.

Good luck