Jump to content
  • Advertisement
ethancodes

C# problem activating and deactivating states

Recommended Posts

I'm attempting to implement my first state machine and I'm running into a problem with unloading the main menu and loading the game scene. I'm kind of learning this as I go and I'm basing it off of a really solid example I have to go off of, but I'm apparently missing something because I'm getting a Null Reference Exception. I'll post the exception and code below. Hopefully someone will see my mistake. Thanks!

 

public class StartButton : MonoBehaviour 
{
	public void OnClickStartPlay ()  //This script is attached to my Start Button in the Main Menu scene
	{
		Managers.Game.SetState(typeof(GamePlayState));
	}	
}

//This is the script for my MainMenuState
public class MainMenuState : _StatesBase 
{
	public override void OnActivate ()
	{
		SceneManager.LoadScene("Start");
	}

	public override void OnDeactivate ()
	{
		SceneManager.UnloadSceneAsync("Start");
	}

	public override void OnUpdate ()
	{
		
	}
}

//This is the code for my GamePlayState
public class GamePlayState : _StatesBase 
{
	public override void OnActivate ()
	{
		SceneManager.LoadScene("Level_01");
	}

	public override void OnDeactivate ()
	{
		SceneManager.UnloadSceneAsync("Level_01");
	}

	public override void OnUpdate ()
	{
		
	}
}

 

nullerror.PNG

Share this post


Link to post
Share on other sites
Advertisement

Got this figured out. My Managers object was not instantiated in the Start scene. I have a question though. Obviously I only ever want one Managers object, which should contain all my managers. In the Hierarchy panel, do I just put it in one scene and then make sure I always start the game from that scene? Or do I put one in each scene?

Share this post


Link to post
Share on other sites

You should normally start from a specific scene or have a scene just with your manager objects that you load in addition to whatever scene your start from, both works and the managing scene gives ya a place for all your objects so it would be easier to find for changes.

Don't forgett to trigger Object.DontDestroyOnLoad function so that your manager objects live forever

Share this post


Link to post
Share on other sites

@Shaarigan I've got an on going issue related to this problem. I added some Debug.Logs in all my state methods so I could see when they were being called. When I start the game and I'm in the main menu, it seems to be working right. Then I hit start and the gameplay screen flashes for a second then goes back to the main menu. After that the debug.Log keeps alternating betwen the Gameplay State update method and the MainMenu State update method. I have no idea how they could both be active at the same time. I've tried all kinds of stuff. I do get this warning (see below) but I'm not entirely sure what it's about. I took the UnloadSceneAsync out of the OnDeactivate method on the MainMenu and that got rid of the warning but didn't change the behavior. Any ideas what's going on here and what the proper way to dispose of the MainMenu scene would be when the game starts?

warning.PNG

Share this post


Link to post
Share on other sites

I removed the UnloadSceneAsync because apparently when you LoadScene in Single mode it automatically gets rid of the other one so the problem was it wouldn't unload the original scene yet because I was trying to do it before the other scene was loaded.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!