Search Unity

My code doesn't work and I don't know why (or where it goes wrong...)

Discussion in 'Scripting' started by Maoo, Nov 23, 2014.

  1. Maoo

    Maoo

    Joined:
    Nov 21, 2014
    Posts:
    4
    basically Im following a tutorial to create this "state machine" as described in the eBook "Learning C# By Developing Games In Unity 3D"

    Anyway, I'm supposed to have a code that works by whenever I press SPACE it should shift state as well as printing out in the console what state it is in. Like this:

    (So here SPACE has been pressed 3 times I think, this is from the book)

    This is when I write the code, nothing happens when I press space... Earlier it would switch 1 state and that was it, not it just writes begin-state twice, and I don't know what I've written wrong... My code looks almost identical to the book and I simply can't pinpoint the mistake, I have 6 scripts, but 2 of them are almost exactly identical to another but just for different states, so I'll just show those where I think the mistake might be in:

    4 Samples of my code
    • StateManager
    • IStateBase
    • BeginState
    • PlayState

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Assets.Code.States;
    4. using Assets.Code.Interfaces;
    5.  
    6. public class StateManager : MonoBehaviour
    7.  
    8. {
    9.     private IStateBase activeState;
    10.  
    11.     void Start ()
    12.     {
    13.         activeState = new BeginState(this);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         if (activeState != null)
    20.         {
    21.             activeState.StateUpdate();
    22.         }
    23.     }
    24.  
    25.     public void SwitchState(IStateBase newState)
    26.     {
    27.         activeState = newState;
    28.     }
    29. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace Assets.Code.Interfaces
    5. {
    6.     public interface IStateBase
    7.     {
    8.         void StateUpdate();
    9.         void ShowIt();
    10.         void StateFixedUpdate();
    11.     }
    12. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using Assets.Code.Interfaces;
    4.  
    5. namespace Assets.Code.States
    6. {
    7.     public class BeginState : IStateBase
    8.     {
    9.         private StateManager manager;
    10.  
    11.         public BeginState(StateManager managerRef)
    12.             /*Constructer*/
    13.         {
    14.             manager = managerRef;
    15.             Debug.Log ("Constructing BeginState");
    16.         }
    17.  
    18.         public void StateUpdate()
    19.         {
    20.             if(Input.GetKeyUp(KeyCode.Space))
    21.             {
    22.                 manager.SwitchState(new BeginState(manager));
    23.             }
    24.  
    25.         }
    26.      
    27.         public void ShowIt()
    28.         {
    29.  
    30.         }
    31.  
    32.         public void StateFixedUpdate()
    33.         {
    34.  
    35.         }
    36.     }
    37. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using Assets.Code.Interfaces;
    4.  
    5. namespace Assets.Code.States
    6. {
    7.     public class PlayState : IStateBase
    8.     {
    9.         private StateManager manager;
    10.      
    11.         public PlayState(StateManager managerRef)
    12.             /*Constructer!*/
    13.         {
    14.             manager = managerRef;
    15.             Debug.Log ("Constructing PlayState");
    16.         }
    17.      
    18.         public void StateUpdate()
    19.         {
    20.             if(Input.GetKeyUp(KeyCode.Space))
    21.             {
    22.                 manager.SwitchState(new PlayState(manager));
    23.             }
    24.         }
    25.      
    26.         public void ShowIt()
    27.         {
    28.          
    29.         }
    30.      
    31.         public void StateFixedUpdate()
    32.         {
    33.          
    34.         }
    35.     }
    36. }
     
  2. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    Your states need to create new instances of the next state, not the current state.

    In your BeginState class, line 22 you have this code:
    Code (CSharp):
    1. manager.SwitchState(new BeginState(manager));
    That should be:
    Code (CSharp):
    1. manager.SwitchState(new PlayState(manager));
    Same in your PlayState class, same line of code. The BeginState should create an instance of PlayState (not another BeginState), and PlayState should create an instance of WonState (not another PlayState).
     
  3. Maoo

    Maoo

    Joined:
    Nov 21, 2014
    Posts:
    4
    That's so annoyingly simple I feel so stupid for not having seen it myself and I've been sitting here looking at it for so long :p Thanks!