Hi guys, I’m reading a Unity C# textbook and I’m trying to figure out the Event Management example there. I think however I’m missing some sort of crucial component. I have this script attached to a game object, but when “health” or “ammo” get too low (I use the mouse keys to reduce their values), nothing is printed in the console (I put Debug.Log there, and in Debug Mode I see that health and ammo do indeed decrease)… how come the console doesn’t print anything?
using UnityEngine;
using System.Collections;
public class EnemyObject : MonoBehaviour
{
//-------------------------------------------------------
//C# accessors for private variables
public int Health
{
get{return _health;}
set
{
//Clamp health between 0-100
_health = Mathf.Clamp(value, 0, 100);
//Check if dead
if(_health <= 0)
{
OnDead();
return;
}
//Check health and raise event if required
if(_health <= 20)
{
OnHealthLow();
return;
}
}
}
//-------------------------------------------------------
public int Ammo
{
get{return _ammo;}
set
{
//Clamp ammo between 0-50
_ammo = Mathf.Clamp(value,0,50);
//Check if ammo empty
if(_ammo <= 0)
{
//Call expired event
OnAmmoExpired();
return;
}
}
}
//-------------------------------------------------------
//Internal variables for health and ammo
private int _health = 100;
private int _ammo = 50;
//-------------------------------------------------------
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
_ammo -= 10;
}
if (Input.GetMouseButtonDown (1)) {
_health -= 20;
}
}
//-------------------------------------------------------
//This event is called when health is low
void OnHealthLow()
{
//Handle event response here
Debug.Log ("OnHealthLow");
}
//-------------------------------------------------------
//This event is called when enemy is dead
void OnDead()
{
//Handle event response here
Debug.Log ("OnDead");
}
//-------------------------------------------------------
//Ammo run out event
void OnAmmoExpired()
{
//Handle event response here
Debug.Log ("OnAmmoExpired");
}
//-------------------------------------------------------
}