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

Question How to setup editor code so the object has a child object set?

Discussion in 'Editor & General Support' started by TheCelt, Sep 27, 2023.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    I have an object that needs to have a child object created and referenced automatically so i don't have to keep remembering to do it.

    How do you do this from edit mode? I tried ExecuteInEditMode but Awake/Start doesn't get called every recompile so i can't use those functions to set it up. There is no Initialise method so to speak to check so what should i use to make sure this setup always occurs correctly without intervention ?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You need an editor entry point for this code to run. For example, you could use a MenuItem to add a right-click entry that runs the needed code to create this object and initialise all child objects.

    If there's perhaps some configuration you want to set up first before creating it, then I would make an Editor Window.

    Or you could write a custom inspector that has a button to generate said objects. Lots of different ways.
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721

    The way i want to set it up is like this:

    GameObject¬
    ComponentA
    ComponentB
    ComponentC

    ComponentA has an editor button which i can click to reload everything. I need ComponentB and ComponentC to be aware of this so they receive the event too and do what they need to do. All in Edit mode not Play mode. The problem im having is when/where do you make ComponentB and ComponentC subscribe to events from ComponentA? It's easy enough to do in Play Mode since i can set them up in Awake but Awake doesn't get called in edit mode even with the attributes it doesn't get called every recompile to make sure its subscribed to the events.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    They don't need to subscribe to anything. Just manually code all this to be initialised. GetComponent still works at editor time.
     
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    They do need to subscribe how else will they know my editor code called an event. Awake/Start doesn't always run in edit mode and OnValidate only runs when an inspector value changes so where do i subscribe these components to listen for event changes?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Just
    GetComponent<T>
    the components and call the necessary methods.
     
  7. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    I don't think you've understood what i am asking here...I'm asking what method to use to initiate code in the components not what function to use to get references to other components. Like i said I can't use Start,Awake or OnValidate - there is no initialising function in edit mode it seems.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Yes, there isn't, hence why I'm telling you to just grab the components and do it manually. You said:
    And the most straight forward is to just get a reference to said components, and call the necessary methods.
     
  9. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    So you saying ComponentA calls B and C rather than C and B listening to A? That seems messy since some times B will be there other times C or both or neither... so i would have to hardcode every component that could potentially attached...since i made my project more compositionally with little dependency.. so A isn't really aware of components next to it
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Well you can compose the necessary implementation via interfaces if necessary. And it's not messy to check for components, especially not since we've had TryGetComponent for years now as well.