Now that we understand the syntax of the get and set property accessors, we can implement them in our manager class for greater efficiency and code readability.
Update the code in GameBehavior, as follows:
public class GameBehavior : MonoBehaviour
{
private int _itemsCollected = 0;
// 1
public int Items
{
// 2
get { return _itemsCollected; }
// 3
set {
_itemsCollected = value;
Debug.LogFormat("Items: {0}", _itemsCollected);
}
}
private int _playerHP = 10;
// 4
public int HP
{
get { return _playerHP; }
set {
_playerHP = value;
Debug.LogFormat("Lives: {0}", _playerHP);
}
}
}
Let's break down the code, as follows:
- We declare a new public variable called Items with get and set properties.
- We use the get property to return...