- Home /
Call function after script compilation?
Is there a way to call a function after Unity compiles the scripts. I would like to check if an object exists in the scene and if it isn't add it. I think its possible if I call the function from another GameObject in scene but I would like to avoid that if possible. I don't think it's possible but thought I would ask in case I am missing something.
I have an answer to your title, but I don't see how the title matches the question. Compilation of what?
Each time scripts are compiled. I just thought Unity doesn't always compile all the scripts so I would probably either have to force compilation somehow.
Unity compiles scripts whenever they change. I don't understand how this could relate to adding an object to the scene whenever a script changes though. I wrote a really crude "Shell Window", and since there is no eval
, the user's command needs to be compiled in order to execute it.
Sorry I don't see how it doesn't relate. Say if I'm creating an API for release and certain gameobjects are needed in the scene for it to function correctly, the user would have to know what objects to add to the scene before hand add them manually. I want to resolve this problem by adding them automatically. I thought the best time would be after the scripts are compiled.
I would suggest that it's up to the developer to signal that they want your functionality by creating those scene objects, not the other way around. Imagine they are building their Loading scene which is just a title and a menu that loads other scenes - why would they want your GameObjects in that scene? Seems to me like your design is wrong. What are you actually trying to build?
Answer by DaveA · Aug 14, 2011 at 05:19 PM
Usually such things are handled via Start or Awake funcs looking for the appropriate objects, issuing a Debug.LogError or you could make it fancier than that. You can detect this in an editor script, but those run at odd times: project load, change in editor script, etc. You could make a menu item like 'Init My System' which does the check on user command.
Answer by tonytopper · Sep 22, 2021 at 08:07 PM
You can do this using Unity's serialization callbacks.
More specifically, you can implement the ISerializationCallbackReceiver
interface and create OnBeforeSerialize
and OnAfterSerialize
methods.
Understanding Unity's serialization stack can be helpful for all sorts of things so I'd recommend digging into it a little bit.
Your answer
![](https://koobas.hobune.stream/wayback/20220612180505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Call GUITexture and script 1 Answer
Return multiple parameter 3 Answers
Variable inside a function's NAME - does it work? 2 Answers
RPC call failed 1 Answer