CS2335
Master_v2
Master_v2
  • Introduction
  • Introduction
    • Introduction
      • Design
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Hero's Journey
  • Unity Basics
    • Unity Editor Windows
    • MonoBehavior - Base-Class
    • Unity Engine - Event Functions
  • Getting Started
    • UI-Elements
    • Animator Controller
      • Animation Steps
    • PlayerController Flow Chart
    • PlayerController Code
      • PlayerController - V1 - S20
      • PlayerController V2 S20
      • PlayerController V3 S20
  • Project 1 - Simple Game
    • Overview
    • Project 1 - Get Started
      • UML Class Diagram
    • Player GameObject
      • PlayerController.cs V2
      • PlayerController.cs V3
    • Create 2D Sprite Prefab: Rock
    • Sorting Layers
  • Project1 Code
    • PickUp PreFabs
    • Player GameObject
    • PlayerController - jump
    • GameData Version1
    • PlayerStats Version1
    • MiniGameManager
      • Logic Diagram
    • Simple Spawner
    • Utility Class
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
    • Project 2 - Starter Assets
    • Project 2
      • State Machine Framework
        • Singleton Pattern
      • StateManager - Singleton Design Pattern
      • IStateBase, BeginState
      • Project 2 -Steps: Create new Scene and State
      • Project 2 - List of Steps
        • Project 2 - Starter Code
  • Project 2 -Dialog
    • Hide_Show_Panel Script
    • Configure TitlePanel, DecisionPanel
    • Simple Dialog Prefab
    • Conversation Scriptable Objects
    • DialogManager_ConvList
    • Image Transitions for Buttons
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
      • Text Input
  • Project2 Resources
    • Visual Novel in Unity-Links
    • Scriptable Object Factory
      • ScriptableObjects
    • Dialog Prefab Packages
  • Project 3 - Overview
    • Branching Story Structures
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • Dictionary: User-Choice Data
      • User-Choices - Example
        • Dictionary Value to Disable Options
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
  • Proj3: Inventory System
    • Inventory-System
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Proj3: Custom UnityEvents
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj3: Mini-Game
    • MiniGame-Overview-Proj3
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 3 - Code Mods
    • Project 3 - Steps
    • Project 3 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
  • C# Language
    • C# Language
      • Variables
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Inheritance
      • Polymorphism
      • Interface
      • Switch-Case
      • List< T >
      • Queue< T >
      • Dictionary
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
      • Delegates
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Troubleshooting Issues - Scene Change
  • Details: StateManager: Add Logic
  • StateManager.SwitchState( GameScene nextScene)
  • SceneXState.cs - StateX Class Files - Implements IStateBase

Was this helpful?

  1. Project 2 - StateManager
  2. Project 2

Project 2 - List of Steps

PreviousProject 2 -Steps: Create new Scene and StateNextProject 2 - Starter Code

Last updated 5 years ago

Was this helpful?

Below is an listing of steps for implementing Scene-Change logic for Project2.

The link below gives more detailed information for Gitbook:

  1. Create a total of 5 scenes (MiniGame counts as one of your scenes)

  2. For each scene: add scene to the Unity Build Settings

  3. Add UI Canvas in Scene

    • Configure: Screen-Space Camera

    • Set camera to: MainCamera

  4. Add UI-Text to create a Scene Label to identify the scene

  5. Add Unique Background Image to each Scene

  6. Add 2 Buttons, to jump to other scenes

    1. Recommendation: make a DecisionPanel prefab: panel w/2 Buttons

  7. Add new scenes to StateManager.cs GameScene enums

  8. Add new Scene/State logic to StateManager in SwitchState( GameScene nextScene )

    • case SomeScene:

      SwitchState(new NextState() );

    • break;

  9. Create a new SomeStates.cs classes, add code so it is similar to BeginState.cs

    • Modify code in SomeScene.cs

    • Add code for IStateBase interface:

    • GameScene Scene

    • InitializeObjectRefs( ){ }

    • Constructor:

      • public SomeScene( ){ }

      • scene = GameScene.someScene;

  10. Modify BeginState - Add UI-Button and code for button-logic to go to SomeScene

    • Define object-reference variable: Button optionBtn1, optionBtn2

    • Initialize variable in: InitializeObjectRefs( );

    • Create custom method for each Button's onClick event:

      • public void LoadSomeScene( ){ }

    • Configure using similar code in BeginState.cs for LoadEndScene( )

    • 1 line of code for to switching scene/state

      • StateManager.instanceRef.SwitchState(GameState.EndScene);

  11. Dialog: Choose a Dialog script and create a Dialog Prefab

    • SimpleDialog.cs -

      • does not use ScriptableObject for Conversation Data

      • Prefab does not include character image

    • DialogManager.cs

      • ScriptableObject in Assets for each Conversation

      • Prefab w/Image

      • Co-routine - displays dynamic typing text

  12. Dialog - Add Dialog to Every Scene except the MiniGame

Troubleshooting Issues - Scene Change

1. StartGame in BeginScene The most important thing to check is that you're always starting your game in the BeginScene. Otherwise, the StateManager will have incorrect code executing, because we hard-coded the starting state in StateManager to be the BeginState.

If it’s a button to jump scenes that’s not working, then there are several things to check.

Check the console when you run the project, if you have any errors, that's good, that is where to start.

There are 2 types of errors you may get.

Console Message: BIG PROBLEMS - scene state mismatch.

These are the things to check:

Details: StateManager: Add Logic

GameScene enums in StateManager, these must match with Build Setting Scenes in Build.

public enum GameScene
{
BeginScene = 0,
Scene2 = 1,
MiniGame =2,
Scene3 = 3,
EndScene = 4
}

StateManager.SwitchState( GameScene nextScene)

Add logic to Switch-case statement for changing to the nextScene

//Public method to switch state using nextScene enum
    public void SwitchState( GameScene nextScene )
    {
        switch (nextScene)
        {
            case GameScene.BeginScene:
                SwitchState(new BeginState());
                break;

            case GameScene.EndScene:
                SwitchState(new EndState());
                break;

//ADD ADDITIONAL LOGIC HERE For Custom Scene/State Logic

            default:
                Debug.Log("No match on SwitchState - scene " + nextScene);
                    break;
        } //end switch-block
    } //end SwitchState

SceneXState.cs - StateX Class Files - Implements IStateBase

  • SceneXState.cs Constructor within a SceneXState.cs file. In the constructor, you need to set the scene correctly, example:

public SceneXState(){
     scene = GameScene.SceneX; //Scene-State Mismatch Error if incorrect
     }
  • Button Logic in the SceneXState file that you are trying to leave from - prior to the error: BIG PROBLEMS comment.

Make sure that: logic is correct for the button event: the scene / state must match. the LoadScene( "SceneName" ) matches the SwitchState( new StateName( ));

  public void LoadScene2()
    {
        Debug.Log("Leaving BeginState going to Scene2State");
        StateManager.instanceRef.SwitchState(GameScene.Scene2); //pass to StateManager
    }
  • Button Initialization Logic Error:

    In the code below, there's a mistake because all logic is being added to the btnOption1.onClick method. Look for these types of errors.

public void InitializeObjectRefs()
    {
        btnOption1 = GameObject.Find("ButtonOption2").GetComponent<Button>();
        btnOption1.onClick.AddListener(LoadEndScene);

        btnOption2 = GameObject.Find("ButtonOption1").GetComponent<Button>();
        btnOption1.onClick.AddListener(LoadScene2); //ERROR HERE

        Debug.Log("BeginState InitializeObjRefs");
    }

Unity Build Settings Open the (file >Build Settings), look at the scene ID’s, compare with the

Create new scene, state
Unity build settings