unity C# - Protected Class That Should Be Accessable

This is my code

protected class DTile {
	bool isWalkable = false;
	int tileGraphicId = 0;
	string name = "Unknown";
}

List<DTile> tileTypes;

void InitTiles() {
	tileTypes[1].name = "Floor";
	tileTypes[1].isWalkable = true;
	tileTypes[1].tileGraphicId = 1;
	tileTypes[1].damagePerTurn = 0;
}

Errors on lines

	tileTypes[1].name = "Floor";
	tileTypes[1].isWalkable = true;
	tileTypes[1].tileGraphicId = 1;
	tileTypes[1].damagePerTurn = 0;

4 errors saying the same thing under different name

Assets/TileMap_D/DTileMap.cs(18,30):
error CS0122: `DTileMap.DTile.name’ is
inaccessible due to its protection
level

Was wondering if anyone can help me with this? Shouldn’t be accessible?

1 Answer

1

bool isWalkable = false;
int tileGraphicId = 0;
string name = “Unknown”;

These are all declared private. Private is the default for C# if nothing else is specified. Try:

public bool isWalkable = false;
public int tileGraphicId = 0;
public string name = "Unknown";

Note that protected won’t help you here. Protected will only be readable from within DTile or anything that inherits from DTile. If you want the variables to be private then you need to initialise them with a constructor inside of DTile.