Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

help to return menu

Discussion in 'Scripting' started by Leandre5, Jun 27, 2019.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.  
    9.  
    10.     [SerializeField]
    11.     private float speed; // definit la vitesse du joueur
    12.  
    13.     private GameObject Manager;
    14.     private Vector2 direction;
    15.  
    16.     protected int oil; // variable pour le nombre de oil
    17.  
    18.     public Animator transitionAnim; // recupere l'animation transition
    19.     public Text TextGold; // recupere texte gold
    20.     public Text TextOil; // recupere texte oil
    21.     public GameObject FindujeuWin; // recuper le game objects qui permet d'afficher la victoire
    22.     public Text TextGoldGagnerWin; // recupere la texte qui permet de voir combien de gold il a gagner
    23.     public GameObject FindujeuDefaite; // recuper le game object qui permet d'afficher la defaite
    24.     public Text TextdeMort; // recupere le text de mort pour modifier le nombre de km ( lvl ) parcouru
    25.     public Text TextGoldGagnerDefaite; // recupere la texte qui permet de voir combien de gold il a gagner
    26.  
    27.  
    28.  
    29.     void Start ()
    30.     {
    31.         FindujeuDefaite.SetActive(false);
    32.         FindujeuWin.SetActive(false);
    33.         Manager = GameObject.Find("GameManager").gameObject;
    34.         oil = 0; // definit le nombre de oil à zero lors du lancement du jeu
    35.         TextOil.text = oil + " / 6"; // definit l'affichage de oil
    36.         TextGold.text = Manager.GetComponent<GameManager>().GoldInGame + " "; // definit l'affichage de Gold
    37.     }
    38.  
    39.  
    40.  
    41.     void FixedUpdate ()
    42.     {
    43.         GetInput();
    44.             Move();
    45.  
    46.         if (Manager.GetComponent<GameManager>().lifeInGame <= 0 )
    47.         {
    48.             MortDuPlayer();
    49.         }
    50.       }
    51.  
    52.  
    53.  
    54.     public void Move ()
    55.     {
    56.         transform.Translate(direction*speed*Time.deltaTime);
    57.     }
    58.  
    59.  
    60.  
    61.     private void GetInput ()
    62.     {
    63.  
    64.         direction = Vector2.zero;
    65.  
    66.         if (Input.GetKey(KeyCode.Z))
    67.         {
    68.             direction +=Vector2.up;
    69.         }
    70.         if (Input.GetKey(KeyCode.S))
    71.         {
    72.             direction += Vector2.down;
    73.         }
    74.         if (Input.GetKey(KeyCode.Q))
    75.         {
    76.             direction += Vector2.left;
    77.         }
    78.         if (Input.GetKey(KeyCode.D))
    79.         {
    80.             direction += Vector2.right;
    81.         }
    82.  
    83.     }
    84.  
    85.  
    86.  
    87.     void OnTriggerEnter2D(Collider2D other)
    88.     {
    89.         if (other.gameObject.CompareTag ("PickUp"))
    90.         {
    91.             other.gameObject.SetActive (false);
    92.             oil = oil + 1;
    93.             TextOil.text = oil + " / 6"; // changer l'affichage de oil
    94.             Debug.Log(oil);
    95.  
    96.         }
    97.         if (other.gameObject.CompareTag ("SpawnPlayer"))
    98.         {
    99.            if ( oil >= 6)
    100.            {
    101.                if ( Manager.GetComponent<GameManager>().levelInGame == 3 )
    102.                {
    103.                    WinDuPlayer();
    104.                }
    105.                 else
    106.                 {
    107.                 Debug.Log("Bravo vous avez trouver toute l'essence");
    108.                 StartCoroutine(Transition());
    109.                 Manager.GetComponent<GameManager>().levelInGame += 1;
    110.                }
    111.            }
    112.             else
    113.             {
    114.                 Debug.Log("Il vous manque de l'essence");
    115.             }
    116.         }
    117.     }
    118.  
    119.  
    120. // Lorsque le joueur Gagne
    121.     public void WinDuPlayer()
    122.     {
    123.         TextGoldGagnerWin.text = Manager.GetComponent<GameManager>().GoldInGame + " ";
    124.         Debug.Log("Find du game");
    125.         FindujeuWin.SetActive(true);
    126.     }
    127.  
    128. // Lorsque le joueur Meurt
    129.     public void MortDuPlayer()
    130.     {
    131.         TextdeMort.text = "Bravo vous avez parcouru " + Manager.GetComponent<GameManager>().levelInGame + " kilomètres. Mais malheureusement vous êtes mort";
    132.         TextGoldGagnerDefaite.text = Manager.GetComponent<GameManager>().GoldInGame + " ";
    133.         FindujeuDefaite.SetActive(true);
    134.  
    135.     }
    136.  
    137.  
    138. // Lorsque le Joueur a recuperer les 6 oils et change de scene
    139.     IEnumerator Transition ()
    140.     {
    141.         transitionAnim.SetTrigger("EndRond");
    142.         yield return new WaitForSeconds(1f);
    143.         SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
    144.     }
    145.  
    146.  
    147.  
    148. }

    Second code for the Button

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ReturnMenuButton : MonoBehaviour {
    7.  
    8.         public GameObject FindujeuWin;
    9.         public GameObject FindujeuDefaite;
    10.  
    11.         public void returnMenu()
    12.         {
    13.                 SceneManager.LoadScene("Menu");
    14.                 FindujeuDefaite.SetActive(false);
    15.                 FindujeuWin.SetActive(false);
    16.         }
    17.  
    18. }
    Thanks for you help !!
     
  2. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    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
    Code (CSharp):
    1. public void returnMenu()
    2.         {
    3.                 SceneManager.LoadScene("Menu");
    4.                 FindujeuDefaite.SetActive(false);   <- this
    5.                 FindujeuWin.SetActive(false);        <- and this
    6.         }
    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
     
  3. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    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.
     
  4. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    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.
     
  5. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    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.