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. Dismiss Notice

How To Spawn GUI Window After Death?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Dogg, Jul 8, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Hello fellow Unity members, I'm having a little problem with my script right now. You see I'm trying to spawn my GUI Window after my Player dies, but so far I haven't been able to get it to work. I know how to spawn it in on a button click, but not when the Player does. I have a GUI Window script here:

    Code (CSharp):
    1. public class GUIWindow : MonoBehaviour {
    2.  
    3.     public Rect windowRect = new Rect(20, 200, 200, 200);
    4.  
    5.     bool showWindow = false;
    6.  
    7.  
    8.     void OnGUI() {
    9.  
    10.         if (GUI.Button(new Rect(20, 20, 100, 50), "Hello World"))
    11.         {
    12.             showWindow = true;
    13.         }  
    14.      
    15.         if (showWindow)
    16.         {
    17.             windowRect = GUI.Window(0, windowRect, WindowFunction, "GAME OVER");
    18.         }
    19.     }
    20.     void WindowFunction(int windowID) {
    21.         GUI.Label (new Rect (20, 20, 100, 50), "Hello World");
    22.  
    23.         GUI.DragWindow (new Rect (0, 0, 200, 20));
    24.  
    25.         if (GUI.Button(new Rect(20, 20, 100, 50), "Hello World"))
    26.             print("Got a click");
    27.      
    28.     }
    29. }
    And this is the script that kills my Player:

    Code (CSharp):
    1. public class resr : MonoBehaviour {
    2.  
    3.     ControllerScript player;
    4.  
    5.     void Start()
    6.     {
    7.         player = GetComponent<ControllerScript>();
    8.     }
    9.  
    10.     void OnCollisionEnter2D (Collision2D col)
    11.     {
    12.         // If the colliding gameobject is an Enemy...
    13.         if(col.gameObject.tag == "Dangerous")
    14.         {
    15.             // check if player is NOT invincible
    16.             if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
    17.             {
    18.                 // Find all of the colliders on the gameobject and set them all to be triggers.
    19.                 Collider2D[] cols = GetComponents<Collider2D>();
    20.                 foreach(Collider2D c in cols)
    21.                 {
    22.                     c.isTrigger = true;
    23.                 }
    24.                 // ... disable user Player Control script
    25.                 GetComponent<ControllerScript>().enabled = false;
    26.                 renderer.material.color = Color.gray;
    27.              
    28.             }
    29.             // else the player IS invincible, destroy all obstacles in the way
    30.             else
    31.             {
    32.                 // destroy the Dangerous object
    33.                 Destroy (col.gameObject); // This is to destroy the obstacles
    34.             }
    35.         }
    36.     }
    37. }
    38.  
    Does anyone know ways on how I can achieve this? This is supposed to be for a Game Over window, so I also need to pause the game, but I think I know how to do that.
     
  2. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    Seems like you could have your "PlayerDied" GameObject with the GUIWindow script attached but disabled and where you:
    Code (CSharp):
    1.  
    2.                 // ... disable user Player Control script
    3.                 GetComponent<ControllerScript>().enabled = false;
    4.  
    You'd do the exact opposite and <GUIWindow>().enabled would = true;
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Thanks for your help. It works, but now how can I stop the game from changing scenes in the build settings? I tried Time.timeScale = 0; but that doesn't stop the scenes from changing(obviously). Do I have to move the scenes around in the build settings? Because every time I do that it always screws it up, and I just tried it and it didn't work.