Hi
When I play my game and die I have a gameobject that activates and serves as a death page for me.
I then press the button to return to the menu but when I restart my game ( play) my death page does not start, while I deactivate the game object when the game is launched.
Any help will be appreciated !
here is the code:
Important is on the Start Void and In MortDePlayer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed; // definit la vitesse du joueur
private GameObject Manager;
private Vector2 direction;
protected int oil; // variable pour le nombre de oil
public Animator transitionAnim; // recupere l'animation transition
public Text TextGold; // recupere texte gold
public Text TextOil; // recupere texte oil
public GameObject FindujeuWin; // recuper le game objects qui permet d'afficher la victoire
public Text TextGoldGagnerWin; // recupere la texte qui permet de voir combien de gold il a gagner
public GameObject FindujeuDefaite; // recuper le game object qui permet d'afficher la defaite
public Text TextdeMort; // recupere le text de mort pour modifier le nombre de km ( lvl ) parcouru
public Text TextGoldGagnerDefaite; // recupere la texte qui permet de voir combien de gold il a gagner
void Start ()
{
FindujeuDefaite.SetActive(false);
FindujeuWin.SetActive(false);
Manager = GameObject.Find("GameManager").gameObject;
oil = 0; // definit le nombre de oil à zero lors du lancement du jeu
TextOil.text = oil + " / 6"; // definit l'affichage de oil
TextGold.text = Manager.GetComponent<GameManager>().GoldInGame + " "; // definit l'affichage de Gold
}
void FixedUpdate ()
{
GetInput();
Move();
if (Manager.GetComponent<GameManager>().lifeInGame <= 0 )
{
MortDuPlayer();
}
}
public void Move ()
{
transform.Translate(direction*speed*Time.deltaTime);
}
private void GetInput ()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.Z))
{
direction +=Vector2.up;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.Q))
{
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive (false);
oil = oil + 1;
TextOil.text = oil + " / 6"; // changer l'affichage de oil
Debug.Log(oil);
}
if (other.gameObject.CompareTag ("SpawnPlayer"))
{
if ( oil >= 6)
{
if ( Manager.GetComponent<GameManager>().levelInGame == 3 )
{
WinDuPlayer();
}
else
{
Debug.Log("Bravo vous avez trouver toute l'essence");
StartCoroutine(Transition());
Manager.GetComponent<GameManager>().levelInGame += 1;
}
}
else
{
Debug.Log("Il vous manque de l'essence");
}
}
}
// Lorsque le joueur Gagne
public void WinDuPlayer()
{
TextGoldGagnerWin.text = Manager.GetComponent<GameManager>().GoldInGame + " ";
Debug.Log("Find du game");
FindujeuWin.SetActive(true);
}
// Lorsque le joueur Meurt
public void MortDuPlayer()
{
TextdeMort.text = "Bravo vous avez parcouru " + Manager.GetComponent<GameManager>().levelInGame + " kilomètres. Mais malheureusement vous êtes mort";
TextGoldGagnerDefaite.text = Manager.GetComponent<GameManager>().GoldInGame + " ";
FindujeuDefaite.SetActive(true);
}
// Lorsque le Joueur a recuperer les 6 oils et change de scene
IEnumerator Transition ()
{
transitionAnim.SetTrigger("EndRond");
yield return new WaitForSeconds(1f);
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
Second code for the Button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ReturnMenuButton : MonoBehaviour {
public GameObject FindujeuWin;
public GameObject FindujeuDefaite;
public void returnMenu()
{
SceneManager.LoadScene("Menu");
FindujeuDefaite.SetActive(false);
FindujeuWin.SetActive(false);
}
}
Thanks for you help !!