Our repeated debug logs are a perfect opportunity to abstract out some code directly into the Character class:
- Add a new public method with a void return type, called PrintStatsInfo, to the Character class.
- Copy and paste the debug log from LearningCurve into the method body.
- Change the variables to name and exp, since they can now be referenced from the class directly:
public void PrintStatsInfo()
{
Debug.LogFormat("Hero: {0} - {1} EXP", name, exp);
}
- Replace the character debug log that we previously added to LearningCurve with method calls to PrintStatsInfo, and click on Play:
Character hero = new Character();
hero.PrintStatsInfo();
Character heroine = new Character("Agatha");
heroine.PrintStatsInfo();
Now that the Character class has a method, any instance can freely access it using dot notation. Since hero and heroine are...