Search Unity

"OnSelected" event in editor

Discussion in 'Scripting' started by SgtLame, Aug 27, 2021.

  1. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    Hi,
    In editor, I need to run some code on a gameobject, at the moment that gameobject is selected in the hierarchy (e.g. when the user clicks on the game object in the hierarchy).
    I thought the OnEnable() method was made for that, but there is obviously something I don't get since I can't get it to work. I also added the [ExecuteInEditMode] statement, but that doesn't cut it.
    Also tried to run code in the Update() Method, testing first if gameObject.activeSelf is true. Problem: gameObject.activeSelf is always true.
    I searched the forum and the doc, but I couldn't find that "OnSelected" method I need. Any ideas? ^^'
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    and there is also OnDrawGizmosSelected when you want to draw gizmos only when the object is selected.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    activeSelf has nothing to do with editor selection, it is whether the object itself is enabled (the checkbox in the top left of the inspector), this is also valid during runtime.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    oh and don't forget that the editor is in immediate mode, that's why you don't need no events.
     
  6. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    Thanks for your answers! I'm gonna give it a try. :)
     
  7. SirMcsquizy

    SirMcsquizy

    Joined:
    Nov 21, 2013
    Posts:
    11
    Also if you need to run a function every Editor Tick (basically Update() )

    you need to put in the start function EditorApplication.update += "function name"
     
  8. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    Thanks a lot!