- Home /
Question by
MIHGames · Jun 08, 2014 at 10:39 PM ·
c#state-machinebooks
OnGui Button not displaying
Hi guys! I'm working through the book "Learning C# by Developing Games with Unity" and have come into some trouble with my state machine. I'm supposed to have a button showing that says Press to play, the game will be paused and then when you press the button the game unpauses.
Here is the code:
StateManager script:
using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;
public class StateManager : MonoBehaviour
{
private IStateBase activeState;
// Use this for initialization
void Start ()
{
activeState = new BeginState(this);
}
// Update is called once per frame
public void Update ()
{
if (activeState != null)
activeState.StateUpdate();
}
public void onGUI()
{
if(activeState != null)
activeState.ShowIt();
}
public void SwitchState(IStateBase newState)
{
activeState = newState;
}
}
Here is the BeginState script:
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class BeginState : IStateBase
{
private StateManager manager;
public BeginState (StateManager managerRef) //Constructor
{
manager = managerRef;
Debug.Log ("Construction BeginState");
}
public void StateUpdate()
{
if (Input.GetKeyUp (KeyCode.Space))
{
manager.SwitchState (new PlayState (manager));
}
}
public void ShowIt ()
{
if (GUI.Button (new Rect (10, 10, 150, 100), "Press to Play"))
{
Time.timeScale = 1;
manager.SwitchState (new BeginState (manager));
}
}
}
}
Here is the IStateBase script
namespace Assets.Code.Interfaces
{
public interface IStateBase
{
void StateUpdate();
void ShowIt();
}
}
Comment
Best Answer
Answer by robertbu · Jun 08, 2014 at 11:02 PM
OnGUI() has an upper case 'O;. You have a lower case 'o'.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Starting out C# help 2 Answers
Learning scripting in C Sharp. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
c# not going to destination on 0 hp 1 Answer