Our characters are going to need good weapons to see them through quests, which are good candidates for a simple struct:
- Create a public struct, called Weapon, in the Character script. Make sure it's outside the Character class's curly braces:
- Add a field for name of type string.
- Add another field for damage of type int:
You can have classes and structs nested within each other, but this is generally frowned upon because it clutters up the code.
public struct Weapon
{
public string name;
public int damage;
}
- Declare a constructor with the name and damage parameters, and set the struct fields using the this keyword:
public Weapon(string name, int damage)
{
this.name = name;
this.damage = damage;
}
- Add a debug method below the constructor to print out the weapon information:
public void PrintWeaponStats()
{
Debug.LogFormat...