- Home /
State problem from tutorial
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");
}
}
}
Answer by gjf · Sep 27, 2014 at 01:30 PM
your formatting leaves a lot to be desired but the problem is most likely in line 17 of StateManager.cs
newState = activeStae;
should be
activeStae = newState;
Answer by Amitr · Sep 27, 2014 at 01:47 PM
Thank you, that solve it. I am now continue to try and connect Touch script to this system so a touch button will affect it.
I am trieng to add a standart touch script that loop thru touches and check if "began" phase has been made.
I dont really sure how to integrate in into this system
Can i create a reference for the button (GUItexture) it self on the state and put the call for that reference in StateUpdate? checking whatever if this button has been press, if so call a method on the button to Switch state \ load scene ? is this a good planning?
thanks !
I have made this now, but for some reason after i press in my android the "PlayGameButton" which is GUItexture, i get this error and the game pause:
"NullReferenceException: Object reference not set to an instance of an object Assets.Code.States.TeamSelectionState..ctor (.State$$anonymous$$anager managerRef) (at Assets/Code/States/TeamSelectionState.cs:19) Assets.Code.States.PlayGameButtonClass.Press () (at Assets/Code/Buttons/PlayGameButtonClass.cs:46) Assets.Code.States.$$anonymous$$ain$$anonymous$$enuState.StateUpdate () (at Assets/Code/States/$$anonymous$$ain$$anonymous$$enuState.cs:36) State$$anonymous$$anager.Update () (at Assets/Code/Scripts/State$$anonymous$$anager.cs:40)"
Those are the States involved:
using UnityEngine; using Assets.Code.States; using Assets.Code.Interfaces;
public class State$$anonymous$$anager : $$anonymous$$onoBehaviour {
private IStateBase activeState;
private static State$$anonymous$$anager instanceRef;
void awake ()
{
Debug.Log ("Awake");
if (instanceRef == null)
{
Debug.Log ("instanceRef = this");
instanceRef = this;
DontDestroyOnLoad(gameObject);
}
else
{
Debug.Log ("Destroied Gameobject");
DestroyImmediate (gameObject);
}
}
void Start ()
{
activeState = new $$anonymous$$ain$$anonymous$$enuState (this);
}
void Update ()
{
if (activeState != null)
activeState.StateUpdate ();
}
void OnGUI ()
{
if (activeState !=null)
activeState.ShowIt();
}
public void SwitchState (IStateBase newState)
{
activeState = newState;
}
}
using UnityEngine; //using System.Collections; //using System.Collections.Generic; using Assets.Code.Interfaces;
namespace Assets.Code.States { public class $$anonymous$$ain$$anonymous$$enuState : IStateBase {
CustomizeButtonClass CustomizeRef;
// OptionsButtonClass OptionsRef;
PlayGameButtonClass PlayGameRef;
private State$$anonymous$$anager manager;
public $$anonymous$$ain$$anonymous$$enuState (State$$anonymous$$anager managerRef)
{
manager = managerRef;
Debug.Log ("Constructing $$anonymous$$ain$$anonymous$$enuState");
CustomizeRef = GameObject.Find ("CustomizeButton").GetComponent<CustomizeButtonClass>();
// OptionsRef = GameObject.Find ("OptionsButton").GetComponent<OptionsButtonClass>();
PlayGameRef = GameObject.Find ("PlayGameButton").GetComponent<PlayGameButtonClass>();
}
public void StateUpdate ()
{
PlayGameRef.Press ();
CustomizeRef.Press ();
// OptionsRef.Press ();
}
public void Switch ()
{
}
public void ShowIt ()
{
}
}
}
using UnityEngine; using System.Collections; using System.Collections.Generic; using Assets.Code.Interfaces;
namespace Assets.Code.States { public class TeamSelectionState : IStateBase { private State$$anonymous$$anager manager; public ChasersSelectionButtonClass ChasersSelectionButtonClassRef;
is the Script that is attached to the GUItexture which is the PlayGameButton
using UnityEngine; using System.Collections; using System.Collections.Generic; using Assets.Code.States;
namespace Assets.Code.States {
public class PlayGameButtonClass : $$anonymous$$onoBehaviour
{
private State$$anonymous$$anager manager;
void Start ()
{
manager = GameObject.Find ("Game$$anonymous$$anager").GetComponent<State$$anonymous$$anager> ();
}
public void Press ()
{
if (Input.touches.Length < 0)
{
Debug.Log ("Touch the buttons retard !");
}
else
{
// check how many touches
for (int i = 0; i < Input.touchCount; i++)
{
if(this.guiTexture.HitTest(Input.GetTouch (i).position))
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
manager.SwitchState (new TeamSelectionState (manager));
}
}
}
}
}
} } I am really stack. Why can't it create a reference?
Thanks !
Your answer
