Search Unity

Enabling UI panel after "x" amount of seconds

Discussion in 'Scripting' started by danieldixon123, Feb 18, 2018.

  1. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
    Hi Im new to scripting and I don't know much so please bear with me. Im making a game and I have a panel with text on it saying "game over". I also have a screen in the room that plays a video of a countdown when the player enters a trigger.

    Screen Shot 2018-02-18 at 23.31.41.jpg

    I want the UI panel with the text saying game over to enable when the video finishes and the countdown reaches zero. Does anyone know a simple way of doing this. Thanks a lot
     

    Attached Files:

  2. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    What is the code for your countdown?
     
  3. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Use Time.deltaTime or Time.time depending on what you need, check unity documentation for a more specific description about those 2 (you will probably need Time.deltaTime anyway).
    You basically could have a float variable "timer", and in the update reduce this variable by the time. When timer is 0 you could make the UI panel appear. Or do the other way around, and have the variable timer += Time.deltaTime and if timer is higher than what you need, something happens...
     
  4. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
  5. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
    Its not code its just a video of a countdown
     
  6. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
  7. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    When you start the video of countdown you start the coroutine. Something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WaitForSecondsExample : MonoBehaviour
    5. {
    6.   Gameobject gameoverPanel;
    7.  
    8.   Void StartVideo ()
    9.   {
    10.      //your code where you start your
    11.      video
    12.     StartCoroutine(EnableGameoverPanel ());
    13.   }
    14.  
    15.   IEnumerator EnableGameoverPanel()
    16.     {
    17.         yield return new     WaitForSeconds(115);//wait for 115 seconds (1:55minutes as your video)
    18.  
    19.         gameoverPanel.SetActive(true);
    20.     }
     
    Eckual and danieldixon123 like this.
  8. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
    Sorry for being annoying but this is what I currently have with the addition of your script. So basically its a screen that plays static and when the player walks up to it and enters the trigger it plays a second video by destroying the screen in front.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class destroy : MonoBehaviour {
    9.     public bool destroyOtherObject, showObject;
    10.     public GameObject objectToDestroy, objectToShow;
    11.     GameObject gameoverpanel;
    12.  
    13.     void OnTriggerEnter (Collider other)
    14.     {
    15.        
    16.      StartCoroutine(EnableGameoverPanel ());
    17.     }
    18.  
    19.     IEnumerator EnableGameoverPanel()
    20.     {
    21.         yield return new     WaitForSeconds(115);
    22.         gameoverpanel.SetActive(true);
    23.     }
    24.  
    25.     {
    26.         if(other.tag == "Player") {
    27.             if (destroyOtherObject){
    28.                 Destroy (objectToDestroy);
    29.             }
    30.             if (showObject) {
    31.                 objectToShow.SetActive (true);
    32.             }
    33.         }  
    34.  
    35.     }
    36.  
    37. }
    I'm probably being really stupid but do you know why I'm getting this error

    Screen Shot 2018-02-19 at 14.55.24.jpg
     
  9. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    You forgot void OnTriggerEnter, which should be added on 24th line.
    EDIT : You should have only 1 OnTriggerEnter.
    Why there is no if statement on the 15th line?
    Add the proper if statements on the first OnTriggerEnter and you shouldnt't have problems.
     
    Last edited: Feb 19, 2018
  10. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
    Okay so I basically want it so that when the player enters the trigger the countdown video starts playing and i also want the coroutine to start counting down when they enter the trigger. Im a complete noob how can I have both things happen when the player enters the trigger?
     
  11. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    If you enter the trigger -> the countdown video start playing AND a coroutine starts.
    tranos already told you how to do it, just carefully read the code he provided you and adjust it to your needs.
     
  12. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. public class destroy : MonoBehaviour {
    7.     public bool destroyOtherObject, showObject;
    8.     public GameObject objectToDestroy, objectToShow;
    9.     GameObject gameoverpanel;
    10.     void OnTriggerEnter (Collider other)
    11.     {
    12.         if(other.tag == "Player") {
    13.             if (destroyOtherObject){
    14.                 Destroy (objectToDestroy);
    15.             }
    16.             if (showObject) {
    17.                 objectToShow.SetActive (true);
    18.             }
    19.         }
    20.      StartCoroutine(EnableGameoverPanel ());
    21.     }
    22.  
    23.     IEnumerator EnableGameoverPanel()
    24.     {
    25.         yield return new     WaitForSeconds(115);
    26.         gameoverpanel.SetActive(true);
    27.     }
    28. }
    Also some basics to get you started:
    • Classes on C# Read it all! but to give you a quick view have this:

    Code (CSharp):
    1. public class Person
    2. {
    3.     // Public Field
    4.     public string name;
    5.  
    6.     // Constructor that takes no arguments. You wont be using constructors in Unity unleass you have a class that doesn't inherit from MonoBehaviour or any other Unity Type, instead you would use Start() and Awake()
    7.     //Constructors are called whenever you create a new instance of this class
    8.     public Person()
    9.     {
    10.         name = "unknown";
    11.     }
    12.  
    13.     // Constructor that takes one argument.
    14.     // This is called "Constructor overloading" which means that you can create a "new Person()" either without passing a name or passing a name "new Person("James")
    15.     public Person(string nm)
    16.     {
    17.         name = nm;
    18.     }
    19.  
    20.     // Method
    21.     public void SetName(string newName)
    22.     {
    23.         name = newName;
    24.     }
    25.    
    26.     // Methods can also be overloaded
    27.     public void SetName(string firstName, string lastName)
    28.     {
    29.         name = firstName + ", " + lastName;
    30.     }
    31. }
    Code (CSharp):
    1. public class Class1 : Monobehaviour
    2. {
    3.     public int publicField = 1;
    4.     private int privateField = 2;
    5.  
    6.     public void Method()
    7.     {
    8.         int localVariable = 0;
    9.         publicField = localVariable;
    10.     }
    11.  
    12.     //Cannot be seen or used by someone else outside of this class
    13.     private void OtherMethod()
    14.     {
    15.         //Can't access localVariable from Method(), it doesn't exist here
    16.         //Can access class fields
    17.         publicField = 5;
    18.         privateField = 3;
    19.     }
    20.  
    21.     private void ThirdMethod()
    22.     {
    23.         // You can call your own methods without restrictions
    24.         Method();
    25.         OtherMethod();
    26.     }
    27. }
    Code (CSharp):
    1. public class Class2 : Monobehaviour
    2. {
    3.     //public field of type Class1, set it on the editor by dragging a Class1 component to this field
    4.     public Class1 otherClass;
    5.  
    6.     public void UsingClass1()
    7.     {
    8.         otherClass.publicField = 3; //You can set public class fields
    9.         // privateField can not be accessed from here
    10.         otherClass.Method(); // You can call public methods
    11.         // localVariable of Method(), however, doesn't exist here
    12.         //OtherMethod() and ThirdMethod can't be accessed from here
    13.     }
    14.  
    15.     //Cannot be seen or used by someone else outside of this class
    16.     private void OtherMethod()
    17.     {
    18.         //Can't access localVariable, it doesn't exist here
    19.         //Can access class fields
    20.     }
    21.  
    22.     private void ThirdMethod()
    23.     {
    24.         You can call your own methods without restrictions
    25.         Method();
    26.         OtherMethod;
    27.     }
    28. }
     
    danieldixon123 likes this.
  13. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    You definitely can't have this:
    Code (CSharp):
    1. public class Class2 : Monobehaviour
    2. {
    3.  
    4.     // This is a method
    5.     void Method1()
    6.     {
    7.         //Do things here
    8.     }
    9.    
    10.      //This is nothing. It doesn't know to which method it belongs
    11.     {
    12.         // Code here
    13.     }
    14.    
    15.      // You can't do this, there can't be two methods with the same name. They don't extend, just write it on the other one
    16.      void Method1()
    17.     {
    18.         // Code
    19.     }
    20.  
    21.     // You can overload it however. They should be similar on purpose like transform.Rotate(Vector3) and Transform.Rotate(Quaternion)
    22.      void Method1(ParameterType parameterName)
    23.     {
    24.         // Code, now using the parameterName parameter
    25.     }
    26. }
     
  14. danieldixon123

    danieldixon123

    Joined:
    Mar 21, 2017
    Posts:
    8
    OH MY GOD!! Thank you so much you are a legend you don't know how happy this made me when it worked