1
\$\begingroup\$

I am creating a real time multiplayer game in Unity 3d. When a game ends I set a static boolean value called "waitingToEndGame" to true. In Update() it waits a few seconds before moving to a new scene like this:

if (waitingToEndGame)
{
    waitTime += Time.deltaTime;
    if (waitTime >= 5f)
    {
        showGameScores = false;
        waitingToEndGame = false;
        if (type == "Multiplayer") MultiplayerController.Instance.LeaveGame();
        GameObject levelManager = GameObject.Find("Level Manager");
        levelManager.GetComponent<LevelManager>().LoadLevel("MainMenu");
    }
    DisableAllMovingObjects();
}

My problem is that even though I am setting the boolean value to true it acts as if it doesn't change and it doesn't make it into the code above to move to the main menu. I even had debug statements printing here as well and they seem to just suddenly stop.

\$\endgroup\$
  • \$\begingroup\$ Please debug you code first and then ask question. Which IDE do you use? Debug will clearly shows you any exceptions and stacktrace. Using information provided we can just guess what is going on \$\endgroup\$ – Dzianis Yafimau Apr 8 '16 at 7:48
  • \$\begingroup\$ @DenisEfimov Of course I debugged before asking the question. I'm using Visual studio. My issue is it works fine when I am testing this in a single player game, but multiplayer has issues and I cannot debug in multiplayer. \$\endgroup\$ – saboehnke Apr 10 '16 at 18:32
  • \$\begingroup\$ I think there is not enough info provided to help with your issue. But you can add logging and see what happens on remote clients. \$\endgroup\$ – Dzianis Yafimau Apr 10 '16 at 18:37
1
\$\begingroup\$

Well the sample you posted looks fine. For sure there is something else going bad. Check if there is any runtime error or any null reference exception. I prefer using coroutines when dealing with time to wait and looks a bit cleaner.

\$\endgroup\$
0
\$\begingroup\$

My issue ended up being that multiplayer was hitting some extra code that I was not seeing when tracing through and I could not debug this as it had to be on a device. I found a solution to logging while using a device here:http://answers.unity3d.com/questions/175161/debugging-android.html

This allowed me to find where I was getting a NullReferenceException so I could easily fix my code.

\$\endgroup\$
-1
\$\begingroup\$

You should move the line, that changes the boolean to false, to the bottom of your if statement. Because the rest will not run after you change the boolean to false, so it has to be moved to the bottom of the if statement. To be executed last.

\$\endgroup\$
  • \$\begingroup\$ Not even close. Just because the boolean changed in the middle of the block doesn't mean that code execution is going to suddenly jump out of the block. \$\endgroup\$ – Draco18s Apr 11 '16 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.