Im using the book unity 2d development as most of the games i want to create are 2d based.int the book we have two scripts.a playerController and a playerLitener.the controller stores an enum of the different states the player will be and uses a delegate event to alert all other game components of the player’s current state he is in.Unexpectedly for some reason it seems no function register to the events in the playerListener script as the event is always null.Here are the two scripts.
PlayerStateController
using UnityEngine;
using System.Collections;
public class PlayerStateController : MonoBehaviour
{
public enum playerStates
{
idle,
left,
right,
jump,
landing,
falling,
kill,
resurrect,
};
public delegate void playerStateHandler (PlayerStateController.playerStates newstate);
public static event playerStateHandler onStateChange;
//use this before initialization
void Awake ()
{
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
//for operation after update
void LateUpdate ()
{
if (onStateChange == null) {
//Debug.Log ("event not registered");
}
float horizontal = Input.GetAxis ("Horizontal");
if (horizontal < 0.0f) {
if (onStateChange != null)
onStateChange (PlayerStateController.playerStates.left);
} else if (horizontal == 0.0f) {
if (onStateChange != null)
onStateChange (PlayerStateController.playerStates.idle);
} else {
if (onStateChange != null)
onStateChange (PlayerStateController.playerStates.right);
}
}
}
PlayerStateListener
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class PlayerStateListener : MonoBehaviour
{
public float moveSpeed = 3f;
PlayerStateController.playerStates currentState = PlayerStateController.playerStates.idle;
Animator playeranimator = null;
void onEnable ()
{
PlayerStateController.onStateChange += onStateChangeAnimation;
}
void onDisable ()
{
PlayerStateController.onStateChange -= onStateChangeAnimation;
}
// Use this for initialization
void Start ()
{
Debug.Log ("loool");
playeranimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
}
void LateUpdate ()
{
onCycleChange ();
}
void onCycleChange ()
{
Vector3 localscale = transform.localScale;
switch (currentState) {
case PlayerStateController.playerStates.idle:
break;
case PlayerStateController.playerStates.left:
transform.Translate (new Vector3 ((moveSpeed * -1.0f) * Time.deltaTime, 0, 0));
if (localscale.x > 0.0f) {
localscale.x *= -1.0f;
transform.localScale = localscale;
}
break;
case PlayerStateController.playerStates.right:
transform.Translate (new Vector3 (moveSpeed * Time.deltaTime, 0, 0));
if (localscale.x < 0.0f) {
localscale.x *= -1.0f;
transform.localScale = localscale;
}
break;
case PlayerStateController.playerStates.jump:
break;
case PlayerStateController.playerStates.landing:
break;
case PlayerStateController.playerStates.falling:
break;
case PlayerStateController.playerStates.kill:
break;
case PlayerStateController.playerStates.resurrect:
break;
}
}
void onStateChangeAnimation (PlayerStateController.playerStates newstate)
{
if (currentState == newstate)
return;
if (!validChangeStatePattern (newstate))
return;
switch (newstate) {
case PlayerStateController.playerStates.idle:
playeranimator.SetBool ("walking", false);
break;
case PlayerStateController.playerStates.left:
playeranimator.SetBool ("walking", true);
break;
case PlayerStateController.playerStates.right:
playeranimator.SetBool ("walking", true);
break;
case PlayerStateController.playerStates.jump:
break;
case PlayerStateController.playerStates.landing:
break;
case PlayerStateController.playerStates.falling:
break;
case PlayerStateController.playerStates.kill:
break;
case PlayerStateController.playerStates.resurrect:
break;
}
currentState = newstate;
}
bool validChangeStatePattern (PlayerStateController.playerStates newstate)
{
bool ret = false;
switch (currentState) {
case PlayerStateController.playerStates.falling:
if (newstate == PlayerStateController.playerStates.landing || newstate == PlayerStateController.playerStates.kill)
ret = true;
else
ret = false;
break;
case PlayerStateController.playerStates.idle:
ret = true;
break;
case PlayerStateController.playerStates.left:
ret = true;
break;
case PlayerStateController.playerStates.right:
ret = true;
break;
case PlayerStateController.playerStates.jump:
if (newstate == PlayerStateController.playerStates.landing || newstate == PlayerStateController.playerStates.kill)
ret = true;
else
ret = false;
break;
case PlayerStateController.playerStates.landing:
if (newstate == PlayerStateController.playerStates.idle || newstate == PlayerStateController.playerStates.left || newstate == PlayerStateController.playerStates.right)
ret = true;
else
ret = false;
break;
case PlayerStateController.playerStates.kill:
if (newstate == PlayerStateController.playerStates.resurrect)
ret = true;
else
ret = false;
break;
}
return ret;
}
}
Why the event is not being fired ? thank you