- Home /
Having a problem with constructors in C#. "ItemType.ItemType(string) is inaccessible due to its protection level"
Sorry if this is more of a C# problem than a Unity problem, but I'm a bit perplexed here.
I'm making an item system for my Unity game, and I'm using C# to do it. I have an abstract class called ItemType, which contains information about a particular type of items(such as its name, its weight, its market value, its id, etc.). I then have to item classes, ItemSword, and ItemCoin, which inherit from the ItemType class. I also have an ItemManager class, which instantiates ItemCoin and ItemSword and automatically assigns them an ID for me. My problem is that I'm getting an error with the constructors when I try to inherit the classes.
The constructor for ItemType takes one parameter, a string called name. When I go to do the constructor for ItemCoin, I make sure it calls the base class using
ItemCoin(string name): base(name){
//Stuff
}
just like it says on this page.
The error is saying that "name" is inaccessible due to its protection level, as if I had made it private. I don't get how this is possible, though, since I'm not giving it any kind of access modifiers because it's a parameter. ItemSword is not giving me this error, but that's likely because the compiler is still stuck on ItemCoin.
When I don't give "base" any parameters, it tells me that ItemType does not have a constructor with 0 parameters. The same thing happens if I don't use "base" at all, or if I don't give it any constructor.
For reference, here is my full source code.
ItemType.cs:
using UnityEngine;
using System.Collections;
public abstract class ItemType{
public int itemID; //The id of this item
public string itemName; //The text name of this item
public int stackSize = 99; //The maximum amount of this item allowed in a stack. Use -1 for infinite
public int maxAllowedInOneContainer = -1; //The maximum amount of this item allowed in a single container. Use -1 for infinite.
public int weight = 0; //The weight of this item
public int marketValue = 0; //The standard price of this item in stores
ItemType(string name){
itemName = name;
}
}
ItemCoin.cs:
using UnityEngine;
using System.Collections;
public class ItemCoin : ItemType {
ItemCoin(string name): base(name){
stackSize = -1;
}
}
ItemSword.cs:
using UnityEngine;
using System.Collections;
public class ItemSword : ItemType{
ItemSword(string name): base(name){
maxAllowedInOneContainer = 1;
stackSize = 1;
}
}
ItemManager.cs:
using UnityEngine;
using System.Collections;
public class ItemManager {
public const int MAX_ITEMS = 3200;
private static ItemType[] itemList = new ItemType[MAX_ITEMS];
public static int numberOfItems = 0;
ItemManager(){
/*When you make a new item, add it to this huge list of item declarations, or else it won't do anything!*/
ItemSword sword = addItem(new ItemSword("Sword")); //Adds the sword item
ItemCoin coin = addItem(new ItemCoin("Coin"));
}
public ItemType addItem(ItemType item){
//Add the item to the list
itemList[numberOfItems] = item;
//Tell the item its id number
item.itemID = numberOfItems;
//Increment the total number of items by one. This will be the id of the next added item.
numberOfItems += 1;
return item;
}
public int findItemID(string name){
//Finds the item id for an item with a given name
bool found = false;
for (int i = 0; i < numberOfItems; i++){
if (itemList[i].itemName == name){
found = true;
return itemList[i].itemID;
break;
}
}
if (found == false){
throw new ItemIDNotFoundException();
}
}
public string findItemName(int id){
if (id >= itemList.Length){
throw new ItemIDNotFoundException();
}
else{
return itemList[id].name;
}
}
public ItemType GetItem(int id){
//Returns a reference(pointer) to the item type with a given id.
return itemList[id];
}
}
Answer by unormal · Feb 27, 2013 at 12:44 AM
You can make the code compile mostly as-is, by making the ItemType constructor protected, so derived classes can see it.
protected ItemType(string name)
... but I think you really just want to make them both public, presuming you want to be able to create one :)
Not providing a scope (public, private, etc) defaults the scope of a constructor/member/field/etc to private.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093246im_/https://answers.unity.com/themes/thub/images/avi.jpg)