Restart Level

1

I have the following question I am doing a Game in Unity 3d (2D) Well I felt the need to create a restart level "not when I die if I can not restart the level at any time since there are some small spaces where the Player could not enter this was created intentionally what I want is when the player gets stuck restart the level "

This is the code I use to call the levels

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EscenaLogica : MonoBehaviour {
    public AudioSource Musica;
    public AudioSource Perdiste;
    public JugadorLogica Jugador;
    public GameObject PuntoInicio;
    public GameObject[] NivelPrefab;
    private int IndiceDeNiveles;
    private GameObject ObjetoNivel;
    private GameObject moneda;
    public int puntaje;
    public Text TextoDeJuego;
    public bool YaSeEscucho = false;
	// Use this for initialization
	void Start () {
        Musica.Play();
        IndiceDeNiveles = 0;
        ObjetoNivel = Instantiate(NivelPrefab[IndiceDeNiveles]);
        ObjetoNivel.transform.SetParent(this.transform);
		
	}
	
	// Update is called once per frame
	void Update () {
        TextoDeJuego.text = "Nivel" + (IndiceDeNiveles + 1) +
            "\nPuntaje: " + puntaje;
        if (Jugador.Perdiste)
        {
            if(!YaSeEscucho){
                Perdiste.Play();
                YaSeEscucho = true;
            }
            TextoDeJuego.text = "Nivel" + (IndiceDeNiveles + 1) +
            "\nPuntaje: " + puntaje+ "\nPerdiste Presione R para reiniciar el nivel";

            if (Input.GetKeyDown("r"))
            {
                puntaje = 0;
                Jugador.JugadorBody.constraints = RigidbodyConstraints2D.None;
                Jugador.JugadorBody.constraints = RigidbodyConstraints2D.FreezeRotation;

                Jugador.gameObject.transform.position = PuntoInicio.transform.position;
                Destroy(ObjetoNivel);
                ObjetoNivel=Instantiate(NivelPrefab[IndiceDeNiveles]);
                ObjetoNivel.transform.SetParent(this.transform);
                Jugador.Perdiste = false;
                YaSeEscucho = false;
            }
        }
        if (Jugador.SiguienteNivel)
        {
            TextoDeJuego.text = "Nivel" + (IndiceDeNiveles + 1) +
               "\nNivel Completado \nPresione R para  el siguiente  nivel";

            if (IndiceDeNiveles == NivelPrefab.Length - 1)
            {
                TextoDeJuego.text = "Completaste el Juego" + "\n presione R para volver a iniciar";
            }
            if (Input.GetKeyDown("r"))
            {
                Jugador.JugadorBody.constraints = RigidbodyConstraints2D.None;
                Jugador.JugadorBody.constraints = RigidbodyConstraints2D.FreezeRotation;
                Jugador.gameObject.transform.position = PuntoInicio.transform.position;
                if (IndiceDeNiveles == NivelPrefab.Length - 1)
                {
                    Destroy(ObjetoNivel);
                    IndiceDeNiveles = 0;
                    ObjetoNivel = Instantiate(NivelPrefab[0]);
                    ObjetoNivel.transform.SetParent(this.transform);
                    Jugador.SiguienteNivel = false;
                }
                else {
                    Destroy(ObjetoNivel);
                    IndiceDeNiveles += 1;
                    ObjetoNivel = Instantiate(NivelPrefab[IndiceDeNiveles]);
                    ObjetoNivel.transform.SetParent(this.transform);
                    Jugador.SiguienteNivel = false;
                 
                }

            }
        }
		
	}
}

I was using this to reload the level the problem is that if for example it is in level 2 and I restart it, it returns me to level 1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReloadLogica : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Application.LoadLevel(Application.loadedLevel);
        }
		
	}
}
    
asked by user90058 21.10.2018 в 19:33
source

1 answer

-1

try this: SceneManager.LoadScene( SceneManager.GetActiveScene().name );

and remember to add this library:

  

using UnityEngine.SceneManagement;

    
answered by 21.10.2018 в 20:25