Search Unity

How to open a UI panel in its current scene from another scene? [Unity 2017.2.0f3]

Discussion in 'Scripting' started by Arjay01, Nov 20, 2017.

  1. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    So the scenario is I wanted to automatically open a UI panel (Death menu) when a player dies (so when the variable int 'lives' = 0) from another scene. At the moment there are no errors in the code (assumingly I coded it wrong but correctly). I tried to code the UI panel to open when the player's lives reaches to 0 however it didn't open the Death menu.

    (MainMenus Script)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. PlayerProtagonist pp;
    6.  
    7. public Transform mainMenu, optionMenu, deathMenu;
    8.  
    9. public void OptionMenu(bool clicked)
    10.     {
    11.         if (clicked == true)
    12.         {
    13.             optionMenu.gameObject.SetActive(clicked);
    14.             mainMenu.gameObject.SetActive(false);
    15.         } else
    16.         {
    17.             optionMenu.gameObject.SetActive(clicked);
    18.             mainMenu.gameObject.SetActive(true);
    19.         }
    20.     }
    21.  
    22. public void DeathMenu()
    23.     {
    24.          if (pm.lives == 0)
    25.         {
    26.           deathMenu.gameObject.SetActive(true);
    27.           mainMenu.gameObject.SetActive(false);
    28.         } else
    29.         {
    30.           deathMenu.gameObject.SetActive(true);
    31.           mainMenu.gameObject.SetActive(true);
    32.         }
    33.     }
    (Player Script)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerProtagonist : MonoBehaviour {
    7. // Limited lives
    8.     [HideInInspector]
    9.     public int lives = 5;
    10.  
    11.     // Death Counter
    12.     public Text livesText;
    13.  
    14.     [HideInInspector]
    15.     public Rigidbody2D rb;
    16.  
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.  
    21.         // Setting the death count to be accurate
    22.         livesText.text = "Lives: " + lives.ToString();
    23. }
    24. }
    Let me know if you also need the screenshot of Inspectors.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well...

    I don't know enough about your setup. But I will say, don't update your Text in Update...Just update it when the value of lives changes.

    Next, your DeathMenu method in both if and else you are setting deathMenu to true. I'm guessing in the else you wanted false.

    Ok, next. If I had to guess, you have a scene with your menus in it. So, you need a trigger for it. Let's say you lose all your lives. For something this simple, I'd probably have the menu in the game scene where I'm dying at so it can just pop up. If you don't want that, then I would say just use a playerpref. It's simple. Set a playerpref and then in your menu scene, check the player pref and turn on whatever menu you want based on it's value.

    You could also use a static variable, but it's really up to you. You're not going to call the method from one scene to the other, you're just going to set a value and use that to determine what menu to display.
     
  3. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    Ok just to let you know, I am new to Unity and just started 2 days ago.
    So yeah about what you said of a playerpref, how do I set it exactly?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Here's the documentation on PlayerPrefs: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
    So, you'll see there you can set a bool or int or what not (and get/read them back) - There should be examples :)

    However, I'm with @Brathnann on probably doing that in the scene itself, with the panel there.
    If you did go to another scene, for the death, maybe it could just be a unique "death scene".. if you really want, then there is no need to set any kind of value (unless you're saving something else*).
    What I mean is, and if I read this correctly, is that there's no need to tell yourself to open a certain death menu if the scene to which you transition... is that death scene :)
    If the (other) scene is multi-purposed (eg: death + other stuff/main menu), then sure you could save a bool and check it on that scene.

    (sorry, I'm a bit tired.:))
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Nothing wrong with being new, we all start somewhere. @methos5k already provided the link, so I'll leave it at that unless you don't understand something with a playerpref. He does also bring up the other point of just having a "death scene" where it's all about showing the player the death menu, but the downside to switching scenes is that you may still have to carry over other data. For example, if you want to show a score or allow the player to try again(know what level they were on).

    Thus the suggestion that if I lose my last life, you just pop up the death menu right there instead.
     
  6. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    If you insist to have separate scene for your death menu, you should consider scene merging. This way you will have two scenes acting as one.
     
  7. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    Alright I will try this thanks :)