- Home /
Change code at runtime
Does unity have some kind of debug tool? Or when I run the game, change some code, and the game will continue with changes I made WITHOUT resetting all variables and breaking the game.
I know it has to be compiled first, but I`m just asking :D
Visual Studio preferably.
Answer by Denvery · Jan 20, 2015 at 01:34 PM
May be this tool will help you? http://unityvs.com/ we use it.
Hehe thanks :) I know but won`t it reset all my variables if I press ctrl+s? That is the only problem of changing code at runtime, saving it and watching the game break since nothing is initialized.
Answer by winxalex · Dec 05, 2018 at 01:35 PM
@Wrymnn As far as I know unity doesn't have such tool. But unity is build to work that way. Meaning you can change the code while runtime, save(Ctrl+s), wait to compile, and then unity will reinit variables. Unity will reinit ONLY serialized variables, marked as PUBLIC or [SerializeField]. Unity won't init runtime created and init variables or objects. So you need to have custom code which will handle DidReloadScript callback and init those. See in practices urself.
[UnityEditor.Callbacks.DidReloadScripts]
private static void OnScriptsReloaded() {
//custom init here
}
I think this is the Best Answer.
It didn't solve the issue yet but it shed the most light on the subject.
In testing this, I found that winxalex's code shouldn't be added to the $$anonymous$$onoBehavior script being edited at runtime. Ins$$anonymous$$d, this code needed to be its own separate script, itself a generic script, not a $$anonymous$$onoBehavior. This generic script then gets a reference to the $$anonymous$$onoBehavior script being edited at runtime. This generic script also includes winxalex's code OnScriptsReloaded(). Inside this method I could then make use of the reference to the $$anonymous$$onoBehavior script being edited at runtime.
Unfortunately, however, in a complex project, Unity still throws NullReferenceExceptions for scripts unrelated to what I'm editing at runtime. I don't understand why or what the solution is.
Thus far, winxalex's statement seems unfortunately true: "As far as I know unity doesn't have such tool." I'll not be attempting to edit my scripts at runtime unless I learn more about what I could do better here to allow runtime editing of scripts.
Exactly OnScriptReload can be for example on some EditorWindow script or probably script with Init On Load, then when event happen you can do GetComponents() and on them call for example function On$$anonymous$$y$$anonymous$$onoTypeWhenScriptReloaded. Other way is to use heavy reflection as LIVITY does when you change function of the class with other version....
Thanks, I hadn't heard of Livity until now. I just read the thread on it - back in 2013 it looked like the future had arrived early! But by the end of the thread, seems like for me it would be best to hold off until something like this is still regularly supported. https://forum.unity.com/threads/livity-the-live-coding-tool-for-unity.195537/page-7
$$anonymous$$y current project has undergone quite a bit of development already and I don't want to introduce this based on comments in the thread. This would be an incredible workflow to have someday.
Answer by logicandchaos · Jul 12, 2020 at 02:58 PM
You can use scriptableObjects to have is so you can change data, swap out data and functions all while game is running and changes are persistent, but I don't think you can change the code live right now, I think that is something they are working on tho.
Your answer
Follow this Question
Related Questions
Changing a second objects material on trigger 0 Answers
Change 'Color Mask' Property At Runtime 1 Answer
How to dynamically change the shape of a mesh during runtime? 1 Answer
[Android] Bad Rendering for whole Canvas UI. Change sprite image run-time after clicked 0 Answers
Is it possible to change the Frame Over Time property of a ParticleEmitter at runtime? 0 Answers