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

Can a public list accept 2 or more variable types?

Discussion in 'Scripting' started by Falcoshin, Dec 11, 2018.

  1. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Let's say I want to make a list that only accepts ints and strings, but I want the elements of the list to be publicly accessible so they can be edited from the editor. Is that possible without making a serializable structure that just has a public int member and a public string member? Because I want the theoretical component to only be able to read one or the other variable type for that list slot if that makes sense.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    A list can only be one type, but you can convert a string to an int with Int32.Parse.
     
  3. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Ok, let's put this another way. I need something like a list that can contain a TimelinePlayable and something I made called DialogInfo. The DialogInfo is just a class with 3 public members like this:

    Code (csharp):
    1. public class DialogInfo
    2. {
    3.     public string text;
    4.     public string speaker;
    5.     public Sprite portrait;
    6. }
    Each element in the "List" would only be able to be either a TimelinePlayable or a DialogInfo. It can't be both or neither which is why I can't just make a structure that has these two as its members. Likewise I need the DialogInfo object in this list to have its own members editable in this list. I'm not sure if that made any sense, but it's suppose to be for cutscene coordination so that the script can switch back and forth between dialog and a playable.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    One word for you, Interfaces.(or inheritance, what ever you prefer)
     
  5. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Yeah, I tried inheritance and it's not working (or at least not the way I thought it would).
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What/how did you try?
    Show us the other class too.
     
  7. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    I tried creating an empty class that was serializable and made DialogInfo inherit from it. DialogInfo is also suppose to be serializable as well.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    OK, so you've got two problems here:

    1) You want a list that can contain two different data types
    2) You want to be able to view and edit the list contents from the inspector


    #1 is kind of weird, so we should start by asking why exactly you want to do this.

    One possibility is that the two types have some common functionality, and you only need to use the stuff that's the same for both of them. In this case, rather than saying the list can contain X and Y, you probably want to say it can contain only Z, where Z is some supertype of both X and Y. (Either Z is a class, and X and Y both inherit from it, or Z is an interface, and X and Y both implement it.) Then the list doesn't need to know that X or Y even exist, it's just a list of Z.

    But if you find yourself writing a bunch of code like "if (list[0] is X) then..." then you're not really relying on polymorphism, you have two actually different types that need to be treated differently. You still can that, but it's usually not the best choice; you should consider redesigning your interface and/or your data processing algorithms.


    For #2, Unity's pre-made inspector can't really handle that. If you make a list of type Z, then try to add elements to it from the Unity inspector, you'll only be able to add new Zs; you can't tell Unity that you really want to add an X or a Y.

    There's a couple ways you might work around this...

    A) You could write your own custom inspector for your class. Then it can do basically whatever you want, but you need to program your own UI for editing it.

    B) Type Z could inherit from some type that Unity knows how to assign references to in the inspector; e.g. MonoBehaviour. Then you can explicitly create your Xs and Ys as totally separate objects, and add references to them to your list of Z by dragging them there.
     
    Joe-Censored, SparrowGS and Falcoshin like this.
  9. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    I didn't know you could write a custom inspector. I might try that and see how it goes.
     
  10. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    B also works it seems.
     
  11. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
  12. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Supposing this works it doesn't seem like something I'd want to do anyway since it'd add a bunch of unnecessary clutter to my assets folder.
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Just put them in a sub-folder.
    ScriptableObjects are extremely useful and powerful. It may take some time to realize all the benefits, but it's definitely worth it.
     
    Last edited: Dec 13, 2018
    N_Murray and SparrowGS like this.