Radu Harabagiu Radu Harabagiu - 2 months ago 6
C# Question

How to reload a level in unity

I've created a simple 2d rock paper scissors game, in which after each player presses a button they are taken to an endgame screen that has two buttons: exit and play again.

The problem is, I can't figure out how to actually restart the game when the user presses the play again button.Here is what I've tried:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class PlayAgain : MonoBehaviour ,IPointerClickHandler{

public void OnPointerClick(PointerEventData eventData)
{
GameObject.FindGameObjectWithTag("Player1").transform.localScale = new Vector3(1,1,1);
GameObject.FindGameObjectWithTag("Player2").transform.localScale = new Vector3(0,0,0);
GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(0,0,0);
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
}

}


The objects with the tags "Player 1/2/GameOverScreen" are panels,GameOverScreen being the current one.This should (if I'm not wrong) reset all the variables to the value they are given by the first script that should play after the scene is loaded.To make sure of this, I've created a scene execution order like this :

Variables ->Events->OnClickRock/Paper/Scissors->PlayAgain

Here are the other scripts:

Variables:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Variables :MonoBehaviour {
static public string Player1Choice;
static public bool Player1Turn=true;
static public string Player2Choice;
static public bool Player2Turn=true;
static public bool GameOver=false;
static public bool Player1Victory=false;
static public bool Player2Victory=false;
static public bool Draw=false;
}


Events

using UnityEngine;
using System.Collections;

public class Events : MonoBehaviour {

void Start()
{
GameObject.FindGameObjectWithTag("Player1").transform.localScale = new Vector3(1,1,1);
GameObject.FindGameObjectWithTag("Player2").transform.localScale = new Vector3(0,0,0);
GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(0,0,0);
}

void Update ()
{
if (Variables.Player1Turn == false) {
GameObject.FindGameObjectWithTag ("Player1").transform.localScale = new Vector3 (0, 0, 0);
GameObject.FindGameObjectWithTag ("Player2").transform.localScale = new Vector3 (1, 1, 1);
}
if (Variables.GameOver == true)
{
if (Variables.Player1Choice == "rock")
{
if (Variables.Player2Choice == "rock")
{
Variables.Draw = true;
}
else if (Variables.Player2Choice == "paper")
{
Variables.Player2Victory = true;
}
else if (Variables.Player2Choice == "scissors")
{
Variables.Player1Victory = true;
}
}
else if (Variables.Player1Choice == "paper")
{
if (Variables.Player2Choice == "rock")
{
Variables.Player1Victory = true;
}
else if (Variables.Player2Choice == "paper")
{
Variables.Draw = true;
}
else if (Variables.Player2Choice == "scissors")
{
Variables.Player2Victory = true;
}
}
else if (Variables.Player1Choice == "scissors")
{
if (Variables.Player2Choice == "rock")
{
Variables.Player2Victory = true;
}
else if (Variables.Player2Choice == "paper")
{
Variables.Player1Victory = true;
}
else if (Variables.Player2Choice == "scissors")
{
Variables.Draw = true;
}
}
if (Variables.Player1Victory == true)
{
print("Player 1 won!");
}
else if(Variables.Player2Victory == true)
{
print("Player 2 won!");
}
else
{
print("It's a draw!");
}
print("Player one chose: " + Variables.Player1Choice);
print("Player two chose: " + Variables.Player2Choice);

GameObject.FindGameObjectWithTag ("Player2").transform.localScale = new Vector3 (0, 0, 0);
GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(1,1,1);
}
}
}


What am I missing?

Answer

This will reload the current scene

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex,LoadSceneMode.Single);