Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Any OnCreate method?

Discussion in 'Scripting' started by blossomgames, Nov 13, 2018.

  1. blossomgames

    blossomgames

    Joined:
    Oct 23, 2013
    Posts:
    30
    MonoBehaviour has methods Awake and Start. It also has OnDestroy. Where is OnCreate? Here's my problem. I want some code to execute once a component is created, but regardless of whether its game object is active or the component. I want some method that would be called always when a component is created in the scene. Is that possible?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
  3. blossomgames

    blossomgames

    Joined:
    Oct 23, 2013
    Posts:
    30
    Neither of these satisfies my needs.
    Awake is called only if the gameobject owning the component is active
    Start is called only if the component is active and the gameobject owning it.
    I expected to have a method that is called regardless of whether the gameobject or component is active.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    When an object is Instantiated there are no methods automagically invoked so if you don't want to use any of the MonoBehaviour built in methods for initialization then you must explicitly call a method on your disabled object after instantiation.
     
  5. blossomgames

    blossomgames

    Joined:
    Oct 23, 2013
    Posts:
    30
    That's what I was afraid of. Okay. Seems like I have to call my own methods in proper places.
    Thanks.