Simulate Button press in JS

Hello,

I want to restart a scene and get the player to the actual game part (My whole game is one scene).

So at the beginning there is a menu with a button play, to actually start playing.

When the player fails, a game-over screen appears with the buttons retry and menu.

The menu-button has this function to be executed:

function OnQuitSinglePlayer(){
	Application.LoadLevel(0);
}

This works as the player wanted to get to the menu and since the game starts with that it is all good.

The retry-button has to restart the scene too, but should also simulate the play-button press, to actually skip the menu-showup:

function OnRetrySinglePlayer(){
	Application.LoadLevel(0);
	b_play.transform.GetComponent.<UI.Button>().onClick;
	
}

However this don’t work.

I think the EventSystems.ExecuteEvent-class is the right direction on this issue.

Any help is appreciated!

Hello…

If you want to use single scene then divide your gameplay in 3 parts.

  1. Main menu
  2. game play
  3. Gameover.

Now make prefab of these three parts.

On scene load, main menu will be there.

Make a single script for each button press attach this script to main camera.

On start button , make this prefab disabled and initialize gameplay. On game over initialize gameover prefab.

On retry button again initialize game play. So scene will not be loaded again.

If you have any doubts you can ask.

Thanks.