Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Differentiating between components of the same type with the Inspector Drag and Drop

Discussion in 'Scripting' started by greg-h, Jan 17, 2014.

  1. greg-h

    greg-h

    Joined:
    Jul 29, 2012
    Posts:
    17
    Hi everybody,

    I have this pretty annoying issue.

    Context (you can skip this if you want; precise problem described below):

    I'm putting together a event-condition-action tool for our designers to implement simple functionality for our game without needing to know how to program. The system is coordinated through the GameObject hierarchy so that Actions will trigger in response to its parent GameObject's Events being invoked. Other parameters to the Actions and Events can be drag and dropped through the inspector variable assignment interface.

    The problem I'm encountering is how to pass arguments from the triggered Event (e.g., when object enters Collider, pass the entering object to the action) but also allows preexisting objects to be assigned transparently. Anyway, the way I decided to solve it is through a DataProvider MonoBehaviour "interface" that can provide the object required by the Action through either an Event output parameter or as an object that already exists in the scene.

    And it all works great except Events need to be able to have multiple output parameters that can each be assigned to a serialized variable of an Action component independently, and...

    Problem:

    When a MonoBehaviour "A" has a public variable of some other MonoBehaviour type "B", you can drag a game object containing a B component into the B variable slot in A's inspector. However, if the game object has multiple components of type "B", there doesn't seem to be a way to differentiate in which one is assigned. And I need to do that.

    Can I without hacky work arounds like adding the scripts and doing the assignments in a specific order?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Yes, it's possible. However, there's a little annoyance... Most components don't have a unique name. Therefore, choosing the right one might be hard.

    As to how to do it;
    • Script an Editor with an Object field.
    • When the object field has an item in it, do GetComponents() on its gameObject with the type of that field.
    • If the Length of the above is longer than 1, draw a dropdown list with the different components.
    • Change the value of the object field with the one selected in the dropdown list.


    Should be a 20-30 mins job.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    spamove and kreso like this.
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Harinezumi likes this.
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Hehe, yeah. No but I think your method would work to, just a tad bit more work :)
     
  6. greg-h

    greg-h

    Joined:
    Jul 29, 2012
    Posts:
    17
    Ohhh both of your answers sort of work.

    I did not know about the inspector trick. Pretty neat, but kind of a pain.

    I would love to use a custom inspector for this, but unfortunately, it relies heavily on inheritance by using many small and functionally independent extensions of a few base classes. I don't really want to have 30-50 copied and pasted custom inspectors laying around, and Unity doesn't support inspector inheritance without some crazy reflection hacks, so that option is kind of out the window.

    I actually just ended up putting the DataProvider components on child gameobjects of the Event. Looks pretty bad, but it works alright. It's more convenient than the alternatives.

    The other solution I'm going to investigate is just recreating the inspector. Apparently it's not very difficult, and it would afford me the customization I want.
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Seriously, I would disagree with that! It does support inheritance... Unless, once again, I don't understand what you mean.

    And because I'm a teasing jerk, I added that feature to my own framework;

    $uZOxmdg.png

    The drowdown list only appear is the GameObject has more than one instance of that field type.

    And in my case, it works for every Object field in every script. :)
     
    Last edited: Jan 17, 2014