help to return menu

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 !!

While i’m not sure of what is causing your problem, i see several thing that could be improved. We can discuss about that if you want.

As for your problem : I understand that you change scene to go to your menu, i guess that you load your game scene from this menu. This mean that everything in your scene (like your gameobjects findujeu win / defaite) should reset. You need to find out what is causing a different behaviour between the first time you load your game scene and the second time.
For example i don’t understand why you are doing

public void returnMenu()
        {
                SceneManager.LoadScene("Menu");
                FindujeuDefaite.SetActive(false);   <- this
                FindujeuWin.SetActive(false);        <- and this
        }

because loading your menu scene should destroy your objects FindujeuDefaite and FindujeuWin aniway. Those objects would appears back, in their initial state, when you load your game scene.

Aniway i’m just guessing here since you don’t give us much info

In fact, to tell you all I added these lines of code to make a test to make sure that my GameObject is disabled and that there is no end of game page.

But even I don’t really understand why it doesn’t deactivate itself as I say when I start in my player controller (i.e. when the game is launched).

Moreover, I’m sure I have a lot to improve since this is my very first game and I’m not really looking to optimize it.

But I really block on this point because it blocks me for everything else and I don’t really understand why when I go back to the menu and restart the game (by pressing the play button on my menu) my death page remains activated while the first time I start the game I don’t have this problem.

For more info the idea of my game and to launch a game and the level it generates more or less randomly.

Once the first level is finished, it is the same one who recharges.

Once the player is dead, I display my death page and then press return to the menu which allows me to return to the menu and then play again.

But when I restart the game after I die, my death page is the first thing I see ( I can’t play)

And it’s just the first part that works once I’m dead I can’t play anymore.

Maybe my scene doesn’t really restart the scene when I press play after I’m dead and that’s why I’m coming straight to my death page.

Finally I found the problem.

The death page was displayed when the player no longer has any life.

But this life doesn’t change between scenes so when I replay my player already had no life left and that’s why the death page was displayed.

I still thank you for taking the time to answer me.