Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Prefab Variant With Derived Class

Discussion in 'Prefabs' started by lorenzofman2, Apr 15, 2019.

  1. lorenzofman2

    lorenzofman2

    Joined:
    Nov 8, 2014
    Posts:
    1
    Hey people!

    Unity nested prefabs are great. No doubt about it.
    Recently I got into a problem where it make a lot of sense to have a prefab, let's name it A and a prefab variant that is B.
    Using Unity nested prefabs it's really easy to do it.
    My prefab A would have a component ClassA.
    My problem was that I wanted B prefab to have a class ClassB that inherits from ClassA.
    ClassB : ClassA

    How can I achieve that?
    Does Unity support this now?

    I couldn't find nothing on the web.

    Just to be clear. I know I can just remove ClassA from prefab B and add ClassB to it, but what I want is ClassA attributes to be reflected in prefab B.

    Thanks!
     
    Jes28 likes this.
  2. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,532
    That's not supported.
     
  3. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    316
  4. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,532
    As with most things that are not supported: Because nobody implemented it.

    It's a complex feature request that needs an interaction between Prefab variants (a form of inheritance) and the completely different system of script class inheritance. It would require a lot of work and a lot of other feature requests have been much more widely requested, so it's not near the top of priorities of things for us to address.
     
  5. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    316
    What about just allowing simple override on m_Script property so we can just replace it with another script like this work earlier?
     
  6. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,532
    Changing a script to another type would in previous versions break the Prefab connection. Since we don't support breaking Prefab connections anymore, this is no longer an option. But even before you would not get updates from the Prefab Asset due to the broken connection. So it has never really worked.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,761
    Is there a reason why m_Script can't be overridden like any other property? The serializer seems to handle switching the script pretty gracefully, replacing fields by their name automatically. So it seems like changing the script causing a disconnect is an added guard rather than something that's a given. Or does dragging a new script in (with the inspector in debug mode) cause a script remove + a script add?

    I guess the hard part is where to store values of fields that are only on the other class? And what to do with fields that are not on the other class?

    Also, attempting to swap the attached script on a variant in prefab mode seems to work - it does all of the correct things. The console just complains that "Disconnecting is no longer implemented", and the change is lost when leaving prefab mode.