To test our implementation, we have to do the following:
- Open an empty Unity scene but make sure it includes at least one camera and one light.
- Add a 3D GameObject, such as a cube, to the scene and make it visible to the camera.
- Attach the BikeController script as a component to the new 3D object.
- Attach the CameraController script to the main scene camera.
- Create an empty GameObject, add the following ClientObserver script to it, and then start the scene:
using UnityEngine;
namespace Chapter.Observer
{
public class ClientObserver : MonoBehaviour
{
private BikeController _bikeController;
void Start()
{
_bikeController =
(BikeController)
FindObjectOfType(typeof(BikeController));
}
void OnGUI()
{
if (GUILayout.Button("Damage Bike"))
if (_bikeController)
_bikeController.TakeDamage(15.0f);
...