Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Cant assign Generic inherited ScriptableObject in inspector after updating to 2020.1

Discussion in 'Editor & General Support' started by Jacksendary, Jul 26, 2020.

  1. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    Hey all, after updating to Unity2020 I've ran into a bit of a curious issues with my scriptableobject event system where I can't assign my objects to inspector if the object is generic

    First of here is my "main" code and base classes

    Code (CSharp):
    1.  
    2. [CreateAssetMenu( menuName = "Events/GameEvent" )]
    3. public class GameEvent : ScriptableObject, IGameEvent
    4. {
    5.     private List<IGameEventListener> _listeners = new List<IGameEventListener>();
    6.     [Button]
    7.     public void Invoke()
    8.     {
    9.         for (int i = _listeners.Count - 1; i >= 0; i--)
    10.         {
    11.             _listeners[i].OnEventRaised();
    12.         }
    13.     }
    14.  
    15.     public void RegisterListener(IGameEventListener listener)
    16.     {
    17.         _listeners.Add(listener);
    18.     }
    19.  
    20.     public void UnregisterListener(IGameEventListener listener)
    21.     {
    22.         _listeners.Remove(listener);
    23.     }
    24. }
    25.  
    26. public abstract class GameEvent<T> : GameEvent, IGameEvent<T>
    27. {
    28.     private List<IGameEventListener<T>> _listeners = new List<IGameEventListener<T>>();
    29.  
    30.     public void Invoke(T value)
    31.     {
    32.         for (int i = _listeners.Count - 1; i >= 0; i--)
    33.         {
    34.             _listeners[i].OnEventRaised(value);
    35.         }
    36.     }
    37.  
    38.     public void RegisterListener(IGameEventListener<T> listener)
    39.     {
    40.         _listeners.Add(listener);
    41.     }
    42.  
    43.     public void UnregisterListener(IGameEventListener<T> listener)
    44.     {
    45.         _listeners.Remove(listener);
    46.     }
    47. }
    48.  
    Next I've inherited the generic game event like this so I can create specialized events with the specific type I wanna parse in the event:

    Code (CSharp):
    1.  
    2. [CreateAssetMenu(menuName = "Events/BoolGameEvent")]
    3. public class BoolGameEvent : GameEvent<bool>
    4. {
    5. }
    6.  

    Now in a different script I'd add this so I can call .Invoke(), Please note this is not the full script but only the related part of it and there is NO errors on my projects:

    Code (CSharp):
    1.  
    2. public class PowerUp : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     GameEvent _event;
    6.  
    7.  
    8.     public void ExecutePowerUp()
    9.     {
    10.         if (_event != null)
    11.         {
    12.             if (_event is GameEvent<int> gei)
    13.             {
    14.                 gei.Invoke(_targetEventValue);
    15.             }
    16.             else if (_event is GameEvent<bool> geb)
    17.             {
    18.                 geb.Invoke(true);
    19.             }
    20.             else
    21.             {
    22.                 _event.Invoke();
    23.             }
    24.         }
    25.     }
    26.  
    27.     private void OnCollisionEnter2D(Collision2D collision)
    28.     {
    29.         if (collision.gameObject.tag == "Player")
    30.         {
    31.             ExecutePowerUp();
    32.             Destroy(gameObject);
    33.         }
    34.     }
    35. }
    36.  

    With all my code out of the way I now add my Powerup script to a gameobject:
    upload_2020-7-27_0-13-56.png

    I've now also created a Scriptableobject of BoolGameEvent as an asset;
    upload_2020-7-27_0-16-23.png

    But now if I open the the object picker from my powerupscript this event is nowhere to be found:
    upload_2020-7-27_0-17-57.png

    And if I try to drag and drop the script on to the script I get this error:
    upload_2020-7-27_0-18-54.png

    All this seems to work fine in unity 2019.4(LTS) but stopped working in 2020


    I've already tried the following to resolve my problem without luck:
    Restart unity and VS
    Reimport All
    Delete the library folder
    Rename The Generic GameEvent<T> class
    Put the Generic GameEvent<T> class into its own file
    Rename the generic GameEvent<T> and put it into its own file
    Recreate the ScriptableObject asset in 2020
    Change the type of _event variable in my PowerUp Script to GameEvent<bool>
    Changed GameEvent<T> to inherit from ScriptableObject instead of GameEvent (and change related code to work with this change)
    Changed SerializeField to SerializeReference (not sure if this is related to generic objects or only collections so had to try)

    What seems to work is:
    Open Unity 2019.4 and assign the GameEvents from there seems to work and be recognized in the 2020 version, but I'm still unable to change the GameEvent



    Does anyone know what the cause of this could be and how I can fix it?

    - Thanks a lot in regard!
     
    Last edited: Jul 27, 2020
  2. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    Still trying to look around for solutions but it seems that there is other "interesting" Generics variable issues in 2020, may have something to do with the new generic serializer?
     
  3. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    I've gotten it to work after I'm created a new project and added all the old files to it as well as deleting all the events and recreating them, I guess something may have gone wrong in the upgrade of the project some how, but oh well I'm back up and running for now at least
     
  4. Pilestedt

    Pilestedt

    Joined:
    Mar 6, 2019
    Posts:
    1
    I'm having this issue as well; I have a base class SettingsItem that is inherited by SettingsWeapon and SettingsMagazine; I can assign SettingsWeapon and SettingsMagazine fine to gameobject scripts that take the SettingsItem format, but not to the base class. Peculiar indeed.
     
  5. The_Tali

    The_Tali

    Joined:
    Mar 16, 2021
    Posts:
    1
    I know that this post is a little bit expired, but maybe nowadays someone struggle with the same problem. For sure you have to keep every scriptable object script in another cs file. Other way you cannot apply scriptable object to serialized field.
     
  6. EmadGh

    EmadGh

    Joined:
    Jun 10, 2009
    Posts:
    147
    I got around working with "Scriptable Objects architecture" and Got stuck with the same problem. Have a base for Weapons and some other classes ( MeleaWeapon, GunWeapon, and more ) inherited from it.

    Can't assign any of them to List<Weapon> ... inside scriptableobject.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,187
    Are they just not showing up in the object picker, or can you not drag and drop them into object fields?

    The Unity object picker can be pretty bad at showing valid options to pick, though you can still drag-and-drop valid types into the inspector.