- Home /
Check if public field set in editor at compilation
Can I add functionality similar to [[RequireComponent]][1] to my public fields? So if public variable not set it cause compilation error.
Thanks!
[1]: http://docs.unity3d.com/ScriptReference/RequireComponent.html
Answer by _dns_ · Jul 15, 2014 at 02:38 PM
Hi, I don't think you can do that at compile time but here is how I would do it: You can use the OnValidate callback: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html
In the OnValidate function, you can check the value of a field or use GetComponent to check if a component is present. In case of error, you can warn the user with a LogError.
OnValidate is called in the editor any time a value is changed or when the object/scene is loaded or reloaded (after each re-compilation for example) so it's very convenient for this kind of check.
Thank you, OnValidate() seems to be a more convinient way than check in the Strart() method.
And OnValidate is called in the editor only which could be a plus
Answer by ViIIain · Jul 15, 2014 at 02:09 PM
Check if it is not null in Start() method
I can do it in Start() and write to the LogError about it or smth similar, but I want to make check at compilation stage. Is it possible?
Your answer
Follow this Question
Related Questions
Compiling Scripts Popup Appears Every Time You Switch to Unity 0 Answers
How can I get more information about this error: "The classes in the module cannot be loaded"? 1 Answer
Isn't there a "do not compile" folder? 6 Answers
Ask for Script Compilation from EditorWindow Button 1 Answer
Measuring script recompilation times 1 Answer