- Home /
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: (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?
Please consider posting code within UA using code tags, and pasting errors here too. Why would Items inherit from Item? That doesn't seem to make sense.
@AlwaysSunny I thought I needed to inherit from Item to use the variables to create new items? I'm still a noob, so I wouldn't be surprised if I was completely off.
Okay, turns out I'm just dumb. The enum had to be outside of the Item class, then the Items class could recognize it without inheritance.
Did you find a way to fix the error having the same problem
Answer by IEMatrixGuy · Nov 13, 2014 at 05:30 AM
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 :)
IEMatrixGuy
http://puu.sh/cOJIz/a62ab54562.png I added one there, if that's what you were talking about and I'm still getting the error.
Answer by Owen-Reynolds · Nov 13, 2014 at 04:39 PM
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.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Behind the scenes voodoo w/ Instantiate 2 Answers
C# Array of Child Objects 1 Answer
Parent Class variables in Child Class's Inspector (C#) 0 Answers
Inheritance best solution to handle *many* instances of a class with unique values? 1 Answer