- Home /
Initialize Fields when Created, not when Loaded
I understand using constructor in a MonoBehavior is a big no-no as Unity has the very bad habit of calling it randomly whenever it feels like it.
However, I need to create subobjects contained within that MonoBehavior when it's first created.
Of course, those subobjects are serialized and should not be recreated when loaded.
I've look at Start() and Awake() and both are not called when I would like it - when the game is running -, and I can't find a "OnCreate" method. Someone have an idea?
Could you provide a bit more info about your exact scenario? Especially, why you don't want to initialize inside Awake?
I don't want to initialize inside Awake, since Awake is called when the game starts. What I want, is initialize when a object is first created in the editor, a default value if you prefer.
I don't know if this will help, but maybe you could use this:
private string someString = CreateString();
private static string CreateString()
{
print("CreateString called.");
return "aaaa";
}
changing the string to your object type. Unfortunately the method that initializes has to be static, and this will be called again after game starts. As a side note - constructor would have been called again as well.
Initializing object field popup some error similar to when you try to use a Constructor. Having it being called on game start is also a deal breaker. It has to be serialized and deserialized properly. The creation of the data must take place only the first time the object is created within the editor.
in this video: https://www.youtube.com/watch?v=$$anonymous$$mUT0ljrHNc
Tim Cooper mentions that you should not use the constructor inside serialized objects, but rather create your object inside OnEnabled(), and check for null there so that it will not recreate itself.
Answer by Benproductions1 · Sep 11, 2013 at 11:03 PM
Hello,
I believe I can help, but due to the internet I am currently using, I can't tell if this has already been said in the comment section.
Anyway, let me make this clear: When you write a script, all you are doing is defining behaviour. The script (as a component) is never "created" before the game is run. This means that your theoretical OnCreated
is the exact same as the already existing Awake
.
I believe what you are looking for is some sort of dependancy system.
Since you have not stated the context in which you need the dependancy, I will explain both methods of achieving it:
Component Dependancy
If you have a scirpt that requires another script or another component to be part of the same object, but you can't be sure that it always gets added, or it just makes it easier, then you can use[RequreComponent(Type)]
. This will automatically attach the component of typeType
when this script is added. This works both when usingAddComponent
in a scirpt or when adding the script to a objectCustom Editor
If you have a component (or anything really) and you want it to be dependant on some sort of heiarchy (or really anything), you can write a customEditor
for you script. This will allow you to quite literally do anything when the script is added to aGameObject
. Note that this will not work in a build, so you can only use it while in editor.
Last but not least you really should just do all this in Awake
. Makes things easy and it does work. Awake
btw is called inside the Instantiate
function, so it gets called before anything else. This is where you make defaults :)
Hope this helps,
Benproductions1
I'm sorry, maybe I wasn't clear enough in my question, but it is for an editor, to create data, which all happens while the game is not running. There was a lot of back and forth in the 12 comments about that. Ends up the method I was seeking is "OnEnable" which is called while the game is not running, on creation and/or on deseralization.
In my specific case, I'm building an event editor and when you create a new instance of the $$anonymous$$onoBehavior containing the event, I want to build some required structures of objects inside it. Obviously, in this case Awake or Start are both useless as they are fired only when the game is launched.