- Home /
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.
at runtime you can change your parameter in inspector you dont need to change in code, and code wont always be correctly updated either. when you exit play mode in unity your changes in the inspector will be gone, so write them down.
if you wanted so backup your data before overwriting them you can output to xml or unitys preference method.
ah xd, it is made on purpose to delete any changes done at runtime, it is basically a play test for you to check the best settings, so it is best to never change too much in play testing, you should use it for small modifications then exit it change it and then play test again
why change scripts during runtime?
I cannot even believe you are asking that xD
at runtime you can change your parameter in inspector you dont need to change in code, and code wont always be correctly updated either. when you exit play mode in unity your changes in the inspector will be gone, so write them down.
if you wanted so backup your data before overwriting them you can output to xml or unitys preference method.
DESI$$anonymous$$A I want to change code not simple parameters in Inspector....Code in scripts like add stuff to function etc. (Like eclipse/java uses with hot-swapping)
You cant do that in unity. Unity will compile everytime you make changes, it is not hot swappable
Answer by babaji1234 · Feb 01, 2015 at 07:13 AM
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
}
Yeah this was pretty good idea, but The only problem I see is with GUI. All GUI breaks, but the textures stay loaded. And I get Null reference exceptions at stupid lines where it should give any errors. Like here:
Xpos = 5;
Ypos = 5
GUI.Label(new Rect(Xpos, Ypos, 20, 50), "Something", myStyle);
//myStyle defined somewhere else
And this entire gui.label will get null or something, like its part of GUI class problem that the GUI class will get crazy.
Answer by team_eden · Feb 01, 2015 at 02:08 AM
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
Haha no xD my game is quite complex to do everything by inspector it isn't option anymore.Yeah I also use load texture. But there are other problems like when you play the game and chage variables during gameplay like select your hero and there is variable that holds selected gameobject. It will reset...