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

Wait for 5 seconds to hide the canvas

Discussion in 'Scripting' started by hii, Apr 7, 2016.

  1. hii

    hii

    Joined:
    Apr 7, 2016
    Posts:
    2
    Hey, I want to deactive car control as car enters the OnTriggerEnter and show warning canvas for 5 seconds. I tried to do coroutine but didnt get it to work. :/ any help? Many thanks:)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class task1 : MonoBehaviour
    7. {
    8.  
    9.     public Canvas passCanvas;
    10.     public Canvas failCanvas;
    11.     public Canvas warningCanvas;
    12.     public Text txtSpeed;
    13.     // public float speed;
    14.     //  private Rigidbody rigid;
    15.     //  bool isTaskOn;
    16.     float maximumAllowed = 33.0f;
    17.  
    18.     private bool missionEnd = false;
    19.  
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         // speed = rigid.velocity.magnitude * 3.6f;
    25.         // isTaskOn = false;
    26.         passCanvas.enabled = false;
    27.         failCanvas.enabled = false;
    28.         warningCanvas.enabled = false;
    29.  
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.  
    36.         float sp = this.GetComponent<RCCCarControllerV2>().speed * 0.6213712f;
    37.  
    38.         if (sp > maximumAllowed)
    39.         {
    40.             this.GetComponent<RCCCarControllerV2>().canControl = false;
    41.  
    42.             missionEnd = true;
    43.  
    44.             //print("you failed"); ;
    45.             failCanvas.enabled = true;
    46.         }
    47.         if (missionEnd)
    48.         {
    49.             this.GetComponent<RCCCarControllerV2>().speed *= 0.98f;
    50.             if (this.GetComponent<RCCCarControllerV2>().speed < 1)
    51.                 this.GetComponent<RCCCarControllerV2>().speed = 0f;
    52.         }
    53.         txtSpeed.text = Mathf.Round(sp).ToString();
    54.         /*  if(isTaskOn)
    55.           {
    56.  
    57.           }*/
    58.     }
    59.  
    60.     private bool hasPassedPoint1;
    61.     private bool hasPassedPoint2;
    62.  
    63.     void OnTriggerEnter(Collider collider)
    64.     {
    65.         if (collider.gameObject.tag == "task1")
    66.         {
    67.             warningCanvas.enabled = true;
    68.            
    69.             //isTaskOn = true;
    70.         }
    71.         else if (collider.gameObject.tag == "pedesterianPoint1")
    72.         {
    73.             hasPassedPoint1 = true;
    74.         }
    75.         else if (collider.gameObject.tag == "pedesterianPoint2")
    76.         {
    77.             hasPassedPoint2 = true;
    78.  
    79.             if (hasPassedPoint1 == false)
    80.             {
    81.                 //fail
    82.             }
    83.             else
    84.             {
    85.                 //sucess
    86.  
    87.             }
    88.         }
    89.  
    90.     }
    91.     void OnTriggerExit(Collider collider)
    92.     {
    93.  
    94.         if (collider.gameObject.tag == "task1")
    95.         {
    96.             print("you won");
    97.             passCanvas.enabled = true;
    98.             this.GetComponent<RCCCarControllerV2>().canControl = false;
    99.             missionEnd = true;
    100.  
    101.         }
    102.     }
    103.     public void playAgain()
    104.     {
    105.         //Application.LoadLevel("testScene 1");
    106.         SceneManager.LoadScene("testScene 1");
    107.         //Invoke("sayHello", 5f);
    108.     }
    109.     void sayHello()
    110.     {
    111.         passCanvas.enabled = true;
    112.         failCanvas.enabled = false;
    113.     }
    114. }
    115.  
     
  2. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Code (csharp):
    1.  
    2. float lockedControls;
    3.  
    4. void Update()
    5. {
    6.     if (lockedControls > 0)
    7.     {
    8.         lockedControls -= Time.deltaTime;
    9.         return;
    10.     }
    11.    
    12.     ...
    13. }
    14.  
    15. void OnTriggerEnter(Collider collider)
    16. {
    17.     lockedControls = 5f;
    18. }
    19.  
     
  3. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    You should use Coroutine, they are perfect for this like

    Coroutine()
    (
    ShowMyPanelScript();
    yield return new WaitForSeconds(5f);
    UnShowMyPanelScript();
    yield return null;
    )
     
  4. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Starting coroutines inside events that can happen twice (in this case enter again on trigger before 5 seconds) can be problematic as it will run 2 "simultaneous" processes.
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Have a look at Panda BT. It's a suitable tool to manage frame-independent logics:

    http://www.pandabehaviour.com

    Let me know if you have any question about using it (I'm the author).
     
  6. hii

    hii

    Joined:
    Apr 7, 2016
    Posts:
    2
    thanks guys for replies, I sorted by using invoke and by disabling car control :)
    Code (CSharp):
    1.  void OnTriggerEnter(Collider collider)
    2.     {
    3.         if (collider.gameObject.tag == "task1")
    4.         {
    5.             warningCanvas.enabled = true;
    6.             Invoke("hideWarningCanvas", 5.0f);
    7.             this.GetComponent<RCCCarControllerV2>().canControl = false;
    8.             missionEnd = true;
    9.         }
    10.     }
    11.     void hideWarningCanvas()
    12.     {
    13.         warningCanvas.enabled = false;
    14.         this.GetComponent<RCCCarControllerV2>().canControl = true;
    15.         missionEnd = false;