Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by rickymanalo · Nov 19, 2021 at 01:55 PM · classlistsabstract

How to make an abstract class with a constructor that needs a List of custom objects?

I'm trying to create an abstract class that needs a List of objects. When I implement it, I get an error saying

 There is no argument given that corresponds to the required formal parameter 'items' of 'SomeClass.SomeClass(List<MonoBehaviour>)'

I can't find the same use case as mine so I asked here.

Here's my abstract class:

 public abstract class SomeClass: MonoBehaviour {
     List<MonoBehaviour> mItems;
     
     protected SomeClass(List<MonoBehaviour> items){
         mItems = items;
     }
 
     public List<MonoBehaviour> GetItems(){
         return mItems;
     }
 
     public abstract GameObject GetView();
 }

And this how I implemented it:

 public class SomeClassHandler : Adapter{
     List<Item> mItems;
 
     public SomeClassHandler(List<Item> items){
         mItems = items;
     }
 
     public override GameObject GetView(){
         return new GameObject();//ignore this, I'm gonna fix this later
     }
 }

Comment
Add comment
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

1 Reply

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

Answer by andrew-lukasik · Nov 19, 2021 at 02:20 PM


  • C# requires you to call base constructor to maintain consistency.


 public abstract class AbstractClass <T>
 {
     List<T> m_items;
     public List<T> Items () => m_items;
     protected AbstractClass ( List<T> items )
     {
         m_items = items;
     }
 }
 
 public class MyClass : AbstractClass<Item>
 {
     public MyClass ( List<Item> items )
         : base( items )
     {
         
     }
 }
 
 public struct Item {}

  • MonoBehaviour constructors are reserved by engine itself. Use Start(), Awake() or OnEnable() for initialization instead.


Comment
Add comment · Show 4 · 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 · Nov 19, 2021 at 02:55 PM 2
Share

Just in case it's not clear from the second point, you can not have a parametrized constructor for MonoBehaviour derived classes since you can never call the constructor manually. The constructor of components is called by Unity and it has to be parameterless.


A common solution when you need to initialize your component externally is to provide your own "Init" method. Declare it virtual in the base class and you can override it in the derived class.

avatar image rickymanalo · Nov 20, 2021 at 11:40 AM 0
Share

How do I use the abstract class as a parameter while also using T as possible? I made a class with a function that needs the abstract class. Now I'm wondering if I can still use generics with the abstract class and, if possible, not making the class that needs the abstract class to be tied to it(I don't know the actual term for it) as I think that would make it more difficult to use it as a component

avatar image andrew-lukasik rickymanalo · Nov 20, 2021 at 01:42 PM 0
Share

You mean, something like this?

 public abstract class AbstractClass <T>
 {
     List<AbstractClass<T>> _items;
     public List<AbstractClass<T>> Items () => _items;
     protected AbstractClass ( List<AbstractClass<T>> items )
     {
         _items = items;
     }
     public void SetItems ( List<AbstractClass<T>> items ) => this._items = items;
     public void AddItem ( AbstractClass<T> item ) => this._items.Add( item );
 }
 
 public class MyClass : AbstractClass<MyClass>
 {
     public MyClass ( List<AbstractClass<MyClass>> items )
         : base( items )
     {
         
     }
 }

KISS > DRY

But I strongly suggest you don't even go there. Syntax complexity compounds and will kill your productivity if you're not careful.

TL;DR: you will work your butt off. Save few lines in this class ("hurray, I feel smarter already!"). And few weeks/months later someone will raise a topic of serialization (networking, save system) and you will despair discovering how (crazy) unnecessarily complicated^3 all of this is to serialize.

avatar image rickymanalo andrew-lukasik · Nov 23, 2021 at 01:12 PM 0
Share

I see, thanks for the info. I'll take that into consideration

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

133 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 avatar image avatar image avatar image avatar image

Related Questions

Help integrating a category system 0 Answers

Problem with arrays in a list 1 Answer

Simple abstractions. Reusing code outside of class 1 Answer

How do I disable a script which contains an abstract class ? 0 Answers

Listing players by score 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