0
\$\begingroup\$

i am trying to play the game without entering to menu screen. i have placed only one scene. my ui and game both is placed within the same scene. in my game over screen i have two buttons. for home button i give

Application.LoadLevel (Application.loadedLevel);

by this code is loading the scene from the begning and its working perfectly. in my another button i want to play the game direcly without showing the main menu. so i tried

Application.LoadLevel (Application.loadedLevel);
launchGame();

but after loading the level my second line launchGame() is not working. how can i solve this problem? please help

\$\endgroup\$
6
  • 1
    \$\begingroup\$ The first line is reloading the scene. Everything in that scene is destroyed. So launchGame() call will not fire. Depending on what you're trying to do, you could make that call from Start() method \$\endgroup\$
    – SanSolo
    Mar 1, 2016 at 6:27
  • \$\begingroup\$ if I do call from start, then every time the game ll start without displaying main menu right? \$\endgroup\$ Mar 1, 2016 at 6:29
  • 1
    \$\begingroup\$ @SanSolo Small detail: launchGame() will be called, it's just unlikely to work as intended, since like you said, the previous scene is already destroyed. \$\endgroup\$
    – piojo
    Mar 1, 2016 at 8:20
  • \$\begingroup\$ @piojo can you please elaborate? \$\endgroup\$ Mar 1, 2016 at 8:23
  • 1
    \$\begingroup\$ @ManojBalajiM I posted an answer. But what I meant by my comment is to point out a common mistake: Unity doesn't follow magical or hidden rules. Unity programming is normal programming, and in normal programming, functions don't exit in the middle, unless you exceptions are thrown or you return early. So when you call Destroy(this), LoadLevel(), or other seemingly magical function, the rest of the code block will still run. \$\endgroup\$
    – piojo
    Mar 1, 2016 at 8:36

1 Answer 1

1
\$\begingroup\$

There are two features you could use to solve this problem: static variables, and DontDestroyOnload(). A static variable is not tied to a certain object, so it will not be erased/inaccessible after reloading the scene. For example, you could add this to a "GameManager" component in your scene:

enum GameState { Menus, Battle };
public static GameState gameState = GameState.Menus;

void Awake()
{
    if (gameState == GameState.Battle)
        launchGame();
}

And your button would set this new state before reloading the scene:

GameManager.gameState = GameManager.GameState.Battle;
Application.LoadLevel(Application.loadedLevel);

The other method to have game logic continue running is to have a game object and script that persists when the level is reloaded. To stay persistent, that script must call DontDestroyOnLoad(gameObject). This technique is more complicated, since after reload, you'll end up with two objects in your scene. You'll need care to ensure the correct object is used. (A loader script is sometimes used for this, so the manager will never be duplicated in the scene--the loader will simply need to be smart enough not to load when not needed.)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .