Structs in C# Question

I am trying to use structs in unity for an inventory system. The struct has all of the variables that will be used for various types of information. I am trying to create a class that copies the struct in order to create a database for each item, so that other scripts can access this database to obtain information for each item. (if there is actually better logic for this please help me out, i have only just started doing anything like an inventory) here’s my code. Please bear with my. I’ve just started structs:

The Struct:

using System;

public struct ItemObject {
		
	private string itemName;
	private string itemDescription;
	private int itemID;
	private string weaponType;
	private int itemDamage;
	private int itemAccuracy;
	private int itemSpeed;
	
	public ItemObject () {
		
		itemName = "name";
		itemDescription = "description";
		itemID = 0;
		weaponType = "melee";
		itemDamage = 2;
		itemAccuracy = 4;
		itemSpeed = 2;
		
	}
	
	public string Name {
		
		get { return itemName; }
		
		set { itemName = value; }
		
	}
	
	public string Description {
		
		get { return itemDescription; }
		
		set { itemDescription = value; }
		
	}
	
	public int ID {
	
		get { return itemID; }
		set { itemID = value; }
		
	}
	
	public string Type {
	
		get { return weaponType; }
		set { weaponType = value; }
		
	}
	
	public int Damage {
	
		get { return itemDamage; }
		set { itemDamage = value; }
		
	}
	
	public int Accuracy {
	
		get { return itemAccuracy; }
		set { itemAccuracy = value; }
		
	}
	
	public int Speed {
	
		get { return itemSpeed; }
		set { itemSpeed = value; }
		
	}
		
}

And the “database”: (barely anything, my problem is that when i try and do this unity says "Unexpected symbol “='”…

using System;

public class ItemDatabase {
	
	ItemObject item0;
	
	item0.Name = "name";

}

And also, needless to say and not to be rude, id prefer not to be treated like a noob. I have a clue to what im doing. I understand a lot of people on these forums see this kind of question and respond in a way such as “well, i think your too inexperienced and should start with something a little simpler.” I am inexperienced in this subject of coding. But I prefer a sophisticated response. Ill know what your talking about.

Thank you in advance for any help!

-myjean17

you can only assign the item0.Name within the constructor or a method of the class!

to better explain what I mean:

using System;

public class ItemDatabase {

    ItemObject item0;

    item0.Name = "name"; //not possible!

    public ItemDatabase(){
        item0.Name = "name"; //possible! (inside ctor)
    }

    public void init()
    {
       item0.Name = "name"; //possible! (inside method)
    }
}

It would seem that item0.itemName is a private member. Try using item0.Name instead.

Also, when you ask noobish questions, you are then treated like a noob. However, I would not call this a noobish question, especially because you seem to understand the concept of constructors and C# properties. This is just a simple mistake that can happen often, much like a simple spelling error in code happens more often than people would like to admit.

You can’t make an assignment like this inside of a class.

using System;
 
public class ItemDatabase {
 
ItemObject item0;
 
item0.Name = "name";
 
}

You need to place it inside of a Method like this.

using System;
 
public class ItemDatabase {
 
ItemObject item0;

void MethodForAssigningStuff()
{
item0.Name = "name";
} 
}

Then you will see that the issue goes away. I’ve been coding for about 9 months but stuff like this slips my mind all the time…
Let my know if this helps… Or if something else goes wrong :expressionless:

Also struct does not allow for a constructor with no parameters.