Hi all. I have asked a qustion about this chapter from the book before, but this time its a different one.
Im working with the book “Learning C# by developing games with unity 3D beginners guid”
I have finished and understand the state chapter 8, and for some reason when i just did simple test it dose not work. I need help.
Problem is:
Play State (2ed state) dose not show update running and update dose not respone.
Log show me:
Constructing BeginState
BeginState Update is running
Constructing PlayState (after i press space)
press space again or any other key dose not give me the scene asked for or the beginstate again.
no “PlayState Update is running” on console.
Please explain me I am fighting with this for days.
Thanks !
code:
namespace Assets.Code.Interfaces
{
public interface IStateBase
{
void StateUpdate ();
void ShowIt ();
}
}
using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;
public class StateManager : MonoBehaviour
{
private IStateBase activeStae;
private static StateManager instanceRef;
void Awake ()
{
if (instanceRef == null)
{
instanceRef = this;
DontDestroyOnLoad (gameObject);
}
else
{
DestroyImmediate (gameObject);
}
}
void Start ()
{
activeStae = new BeginState (this);
}
public void Update ()
{
if (activeStae != null)
activeStae.StateUpdate ();
}
void OnGUI()
{
if (activeStae!=null)
activeStae.ShowIt();
}
public void SwitchState (IStateBase newState)
{
newState = activeStae;
}
}
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class BeginState : IStateBase
{
private StateManager manager;
public BeginState (StateManager managerRef)
{
manager = managerRef;
Debug.Log ("Constructing BeginState");
}
public void StateUpdate()
{
Debug.Log ("BeginState Update is running");
if (Input.GetKeyUp (KeyCode.Space))
{
Switch();
}
}
public void ShowIt()
{
}
void Switch ()
{
Application.LoadLevel ("Scene2");
manager.SwitchState (new PlayState (manager));
}
}
}
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class PlayState : IStateBase
{
private StateManager manager;
// Use this for initialization
public PlayState (StateManager managerRef)
{
manager = managerRef;
Debug.Log ("Constructing PlayState");
}
public void StateUpdate()
{
Debug.Log ("PlayState Update is running");
if (Input.GetKeyUp (KeyCode.Space))
{
Switch();
}
}
public void ShowIt()
{
}
void Switch ()
{
Application.LoadLevel ("Scene3");
}
}
}