0

I have a list in RoomSO that contains premade ButtonSOs that i created in the editor and dragged and dropped into the list. RoomSO is also premade (made in the editor) Then at runtime i create an instance of a ButtonSO and tried adding it to RoomSO buttons. I look at the RoomSO in the editor and i get "Type mismatch". I can´t understand why?

RoomSO script:

 [CreateAssetMenu(fileName = "New Room", menuName = "Rooms/Room")]
 public class RoomSO : ScriptableObject
 {
     public List<ButtonSO> buttons;
 
     public void AddButton()
     {
         ButtonSO bt = (ButtonSO) ScriptableObject.CreateInstance<ButtonSO>() as ButtonSO;
         bt.buttonText = "Hello";
         buttons.Add(bt)
     }
 }

My ButtonSO script:

 [CreateAssetMenu(fileName = "New Button", menuName = "Rooms/Button")]
 public class ButtonSO : ScriptableObject
 {
     public string buttonText;
 }
6
  • Well that roomso script wouldn’t compile. So it’s not all relevant code. So. It’s hard to help Commented Sep 16, 2020 at 8:16
  • added ButtonSO bt = that got missed in the original code. Commented Sep 16, 2020 at 8:33
  • still using (ButtonSO) and as ButtonSO looks quite redundant ... especially since CreateInstance<T> already returns the type T ... My guess would be that it says type mismatch since this is a runtime created instance of a ScriptableObject that only exists temporary in the memory unless you actually save it as an asset .. I don't really understand your use case but if you never actually save this as an asset ... why does it need to be a ScriptableObject instead of a normal class? Commented Sep 16, 2020 at 8:35
  • @derHugo yes, it´s redundant code...but it´s part of a testcode i was trying to get it to work...so that is the explanation of redunant code. I am trying to understand why this isn´t working. I don´t understand why an instance of SO in memory can´t be added to a list to holds the same ButtonSO from assets files. Commented Sep 16, 2020 at 8:42
  • Well it can be added ... I bet it only says mismatch in the Inspector, right? In this case you don't have to worry about it at all ... as said it is only due to not being an actual asset reference but only an object in the temporary memory but during runtime this is no problem for you. If, however, you intent to use this within the Editor to autogenerate some assets then you should make sure you actually store these as assets and reference these instead ... Commented Sep 16, 2020 at 9:16

1 Answer 1

1

You issue isn't really an "issue".

What happens is that usually Unity expects a ScriptableObject asset reference assigned to the fields in the Inspector. Because the actual use-case of ScriptableObject is to have certain exchangeable data/behaviour container assets.

What you are doing is creating instances on runtime. These instances are only stored in the temporary memory since you never actually store them as assets into your project.

You will see the type mismatch in the Inspector, but actually this means there is a valid reference - otherwise it would say either None (ButtonSO) or Missing (ButtonSO), it only won't be saved once the Playmode ends. The later Missing (ButtonSO) you will see after ending the Play Mode since the List<ButtonSO> will still exist, also the items within it - but the according references you created during runtime will be destroyed.

Your runtime code should still work as expected as long as the Play mode is running.

The same happens btw if you assign e.g. a GameObject field within a ScriptableObject with a reference from the Scene during runtime.


In general though: If you create these instances on runtime - why do they need to be ScriptableObjects? You could just use a normal class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that explanation. My code is not in a real project. But i used SO in my real project and came up with some things i didn´t understand about SO. My initial thought where there was some serilazation issue (i have not read up on that). But i broke code of the real project to see what happens with the test code. And then i came upon this "issue"...i was not expecting a "Type mismatch" error at all. It is really not a "Type mismtach" error since the type is actually correct. And i can work with the object as long it is in the memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.