• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by aylwinf · Jul 09, 2015 at 04:16 PM · switchfsmcase

How to implement a Game Manager using Switch and Case in C#

Hi, I'm looking for some help in setting up a Finite State Machine using the Switch and Case statements for a Game Manager to switch between Scenes.

I'm NOT looking to make a FSM for a character (There are a lot of good links out there for this), but I'm looking to build the Game Manager and how I would switch from scene to scene and use the new scene's update function without having to revert back to the Game Master class's update function.

This is what I have working so far: 1. In Game Master Class I have a enum list with all the difference scenes/levels (eg. Splash, MainMenu, Level01, Leve02...) 2. I set the enum case to SPLASH (for Splash Page) in the GameManager's Start Method. 3. Within Case:SPLASH implement the SPLASH class through an interface called ILevelBase. 4. In the SPLASH class I call applicationLoadLevel and load the SPLASH scene file.

This is where I am stuck. I want the game loop to reside within the SPLASH class update function, and not jump back to the GameManager class after constructing the SPLASH scene. How do I make the game loop reside in the SPLASH Scene/Class?

Thanks for your help. A.

Here is my code for my GameManager Class:

 using UnityEngine;
 using Assets.Scripts.Levels;
 using Assets.Scripts.Interfaces;
 using System.Collections;
 
 
 public class GameManager : MonoBehaviour 
 {
 
     private static GameManager gameManagerInstanceReference;
     public ILevelBase activeLevel;
 
     // All the Levels in the game.
     public enum LEVEL
     {
         NULLLEVEL,
         SPLASH,
         MAINMENU,
         LEVEL01,
         LEVEL02,        
         HIGHSCORE,
         CREDITS
     };
     public LEVEL level;    // Use this to check the current level and set the current level.
 
 
 
 
 
     void Awake ()
     {
         // Check to see if the current Game Manager object exists. If it does then destroy.
         if (gameManagerInstanceReference == null) 
         {
             gameManagerInstanceReference = this;
             DontDestroyOnLoad (gameObject);
         }
         else
         {
             DestroyImmediate(gameObject);
         }
     }
 
 
 
     // Use this for initialization
     void Start () 
     {
         //We are in the START level. We now load the SPLASH Level for Logo and animation, and a countdown.
         LevelChange(LEVEL.SPLASH);
     }
 
 
     void Update()
     {
         // Runs the Level's Update Method
         activeLevel.LevelUpdate();
     }
 
 
 
 
 
     void LevelChange(LEVEL newLevel)
     {
         level = newLevel;
 
         // LEVELS FSM
         switch (level) 
         {
         case LEVEL.SPLASH:
             // load the Splash Level
             activeLevel = new Splash(this);
             break;
 
 
         case LEVEL.MAINMENU:
             // The currentLevel variable will be switched to MainMenu from the Splash Level Script.
             break;
 
         case LEVEL.LEVEL01:
             // Load leevl 01 - After the Player Selects "Start" from the main Menu, the currentLevel variable will be assigned Level01
             break;
 
         case LEVEL.LEVEL02:
             // Load leevl 02 - After the Player Selects "Start" from winning Level 01
             break;
 
         case LEVEL.HIGHSCORE:
             // Play the High Score. Set the currentLevel variable to Credits
             break;
 
         case LEVEL.CREDITS:
             // Roll Credits and then set the currentLevel variable to Main Menu.
             break;
         }
     
     }
 }
 

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

I would like some help refactoring my code, I am working with switch cases concering the delivery of food items in my game 1 Answer

Collision detection with switch/case 1 Answer

Syntax of switch + case insensitive? 2 Answers

How to check if an enum’s case has changed 4 Answers

What is a more efficient way to write this Switch Statement? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges