Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CAJJI · Mar 16, 2020 at 10:40 PM · listlistsmonobehavioursingletonconversion

Creating a list of singletons?

Is it possible to create a list of Singletons? For example, if I have a bunch of different types of Manager Singletons, but want to be able to sort through a list of them, can I do that?

I've tried:

 public List<Manager<T>> managers; //where T : MonoBehaviour
 
 public void AddManager<T>(Manager<T> manager) where T : MonoBehaviour
     {
         managers.Add(manager);
     }

but it can't seem to do the conversions when trying to add to the list; even though they're both technically of the same type. I'm starting to think its just not possible.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Mar 17, 2020 at 10:46 AM 1
Share

I'm actually not sure why you would want to keep your singletons in a list, when singleton's are there to make it easier to globally access an instance of your class when you only need one instance.

With the list you are just making this harder for yourself to access the right instance. Because you need to additionally search through the list to find the right manager.

$$anonymous$$aybe your singleton approach just isn't the correct way for what you want to achieve. You could then just aswel keep an instance of each class in a list, ins$$anonymous$$d of a singleton.

$$anonymous$$aybe look into abstract classes or interfaces to suit your needs. But I would not add singletons to a list to further complicate access these instances.

avatar image CAJJI ShadyProductions · Mar 17, 2020 at 06:03 PM 0
Share

I want a list so I can loop through them, that's all. They have a variable indicating if they're active, so I'd rather loop a list than check if each individual singleton is active.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Namey5 · Mar 17, 2020 at 05:16 AM

It's very difficult to understand this kind of thing from the outside without knowing the implementation - even more so when generics come into play. For instance, where and how is the type 'T' defined for use in the list? Either way, chances are you can get there through inheritance. First, declare a base class as abstract without an implementation, then inherit from it for the generic and use the base class as the list type;

 public abstract class Manager { }
 
 public class Manager<T> : Manager
 {
     public T testVar;
 }
 
 ...
 
 public List<Manager> testList;
 
 public void AddManager<T> (Manager<T> man) where T : MonoBehaviour
 {
     testList.Add (man);
 }

https://stackoverflow.com/questions/353126/c-sharp-multiple-generic-types-in-one-list

From there, you can then cast back when you need to.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 17, 2020 at 12:31 PM 1
Share

Right. People always have the wrong idea about what generics are. A generic type with two different type arguments is not related in any way and the two concrete classes are (usually) not compatible. A List<ClassA> and a List<ClassB> are two seperate types no matter if both type arguments are derived from a common class or not.


In regard to covariance and contravariance you could restrict the usage of the type argument for only inward or outward dataflow in which care there is restricted compatibility. However any generic with an in-out type parameter is neither co- nor contravariant. This is for example the case with the List<T> type. For more information on co- / contravariance see my answer over here.

avatar image CAJJI · Mar 17, 2020 at 06:30 PM 0
Share

This is very close to the correct answer (pretty much it), so I will mark it correct. If you are using the standard Singleton<$$anonymous$$onoBehaviour> script this won't exactly work because if $$anonymous$$anager is derived from Singleton<T>, you'll lose the Singleton functionality, but I feel you're aware of that and this is only an issue if someone is doing as described above.

However, this can be solved if you create a Singleton class specifically for $$anonymous$$anagers, where T is $$anonymous$$anager, and not $$anonymous$$onoBehaviour.

Below I've written a test example that may help others further understand how they can do this:

 //Contains any variables you want any manager to have.
 public class $$anonymous$$anager : $$anonymous$$onoBehaviour
 {
     public bool active;
 }
 
 //The Singleton class specifically for $$anonymous$$anagers.
 public class $$anonymous$$anager<T> : $$anonymous$$anager where T : $$anonymous$$anager
 {
     public static T Instance { get { if (!_instance) _instance = FindObjectOfType<T>(); return _instance; } }
     static T _instance;
 }
 
 //Whatever class you want to have a list of $$anonymous$$anagers.
 public class $$anonymous$$anagerList : $$anonymous$$onoBehaviour
 {
     public static List<$$anonymous$$anager> managers = new List<$$anonymous$$anager>();
 
     public $$anonymous$$anager GetFirstActive$$anonymous$$anager()
     {
         foreach($$anonymous$$anager manager in managers)
         {
             if (manager.active)
                 return manager;
         }
         return null;
     }
 }
 
 //An example of a $$anonymous$$anager. Adds itself to the list.
 public class Dialogue$$anonymous$$anager : $$anonymous$$anager<Dialogue$$anonymous$$anager>
 {
     public void Start()
     {
         $$anonymous$$anagerList.managers.Add(this);
     }   
 }

I should also note that this applies to any example where you need a list of Singletons. I simply used $$anonymous$$anagers as an example to help convey my situation and give context to discover a solution.

Thanks for the help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Start function on a listed non mono script? 2 Answers

A node in a childnode? 1 Answer

How do I fix this error when trying to use the Watson SDK for Speech to Text? 0 Answers

Problem with arrays in a list 1 Answer

Affect every object in array. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges