Having problem with constructor

I’m making an inventory system and I want to code all the items in manually using a constructor, I find it more fun that way, but I’m getting an error.

Item Class:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Item {
	public string itemName;
	public string itemDesc;
	public Texture2D itemIcon;
	public int itemID;
	public int itemValue;
	public ItemType itemType;
	
	public enum ItemType {
		Weapon,
		Apparel,
		Consumable,
		Misc,
		Quest
	}
	
	public Item (string name, string desc, Texture2D icon, int id, int value, ItemType type) {
		itemName = name;
		itemDesc = desc;
		itemIcon = icon;
		itemID = id;
		itemValue = value;
		itemType = type;
	}
}

Items List:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Items : Item {
	public static Texture2D asdf;
	
	private Item bread = new Item("bread", "eat it", asdf, 0, 2, ItemType.Consumable);
}

I’m inheriting “Item” in my Items list script. I though I had everything correct, but I get this error: error
(The type “Item” does not contain a constructor that takes ‘0’ arguments). I understand what it’s saying, I just don’t know how to fix it. I tried working around it, changing names and stuff, but nothing works. If you can see what I’m trying to do, can you please lead me in the right direction?

Hi,

As far as I can see it’s actually a pretty simple error.

An enum is not a function (as im sure you are aware), and hence requires for there to be a semi-colon after the line is finished, so after the closing }. This is probably why youre getting that error, because as far as the code is concerned, there is no valid constructor whatsoever.

Hope that helps :slight_smile:

IEMatrixGuy

This is a standard C# error – nothing to to with Unity. There are lots of places to read how C# classes work, and what the technical terms and errors mean.

In general, if it doesn’t involve a specific Unity function, like rayCast or SendMessage, the best place to find the answer is a general google search with C# and the error. Sometimes even if it does involve something Unity-specific, it’s still just a common C# error. Think of Unity as middle-wear. There’s a bunch of stuff everyone knows already, with dozens of helpful places to learn, like modelling, textures, C# programming; and Unity mostly just combines them in a pretty IDE.

Now, in order to learn anything, you need some “real” project to try it on. And nothing wrong with using Unity to learn how to write classes. But I think you’ll be a lot happier if you think of your current project that way – “teaching myself C# classes.” For fun, try the sentence “teaching myself C# classes, and enums, and Unity-C# addons, and making an inventory system.” Doesn’t roll off the tongue.