Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LightStriker · Sep 11, 2013 at 07:32 PM · monobehaviourfieldconstructors

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?

Comment
Add comment · Show 26
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Sep 11, 2013 at 08:22 PM 0
Share

Could you provide a bit more info about your exact scenario? Especially, why you don't want to initialize inside Awake?

avatar image LightStriker · Sep 11, 2013 at 08:29 PM 0
Share

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.

avatar image ArkaneX · Sep 11, 2013 at 08:45 PM 0
Share

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.

avatar image LightStriker · Sep 11, 2013 at 09:08 PM 0
Share

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.

avatar image zombience · Sep 11, 2013 at 10:08 PM 2
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

  1. 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 type Type when this script is added. This works both when using AddComponent in a scirpt or when adding the script to a object

  2. Custom 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 custom Editor for you script. This will allow you to quite literally do anything when the script is added to a GameObject. 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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LightStriker · Sep 11, 2013 at 11:09 PM 0
Share

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.

avatar image Benproductions1 · Sep 13, 2013 at 07:00 AM 0
Share

You could also use [RunInEditor] in some cases

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

constructor called multiple times 1 Answer

How does one go about creating a list of GameObjects? 1 Answer

bool field initialization ignored 0 Answers

Why child ScriptableObjects class instance cannot be placed in their parent ScriptableObject field in the inspector? 1 Answer

Changing friction to slow down swinging of hinge joint quicker 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges