Section 10: Lecture 51: Using Constructors for Nested Classes?

Just a quick question, I noticed we are using nested Classes more in this course than the previous Combat Course and I wandering if there’s a reason not to use constructors with our nested Classes.

For Example the Nested Class DockedItemSlot:

private class DockedItemSlot 
        {
            public ActionItem item;
            public int number;

            public DockedItemSlot() {}
            
            public DockedItemSlot(InventoryItem item, int number)
            {
                this.item = item as ActionItem;
                this.number = number;
            }
        }

Now would this cause some performance issue later down the line, is this bad programming practice, is this just a style consistency thing, or is it one of those weird Unity quirks that causes a problem later down the line?

There’s not a problem with this at all, and I’d say it’s probably a best practice to do so. If you are not creating a parameter based constructor, there is no need to create a blank constructor (because the compiler creates one for you).

1 Like

Sometimes it throws an error when I create an overload constructor in vs code so I just implicitly create a default one out of habit.

It never hurts to create a default constructor.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms