Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

turning on and turnig off indicators

Discussion in 'Scripting' started by Smagacz, Apr 22, 2017.

  1. Smagacz

    Smagacz

    Joined:
    Feb 10, 2016
    Posts:
    23
    Hi,
    I wanto add car indicators to my game. After click any button indicator should turned on.
    Is it good to do animation attached to gameobject with indicator immage or script with mask?
    How to make coroutine which waiting for button press?
     
  2. Smagacz

    Smagacz

    Joined:
    Feb 10, 2016
    Posts:
    23
    I made something like this:
    Code (CSharp):
    1.     private Mask mask;
    2.  
    3.     private IEnumerator coroutine;
    4.  
    5.     public  bool keyPressed = false;
    6.  
    7.     public KeyCode key;
    8.  
    9.     void Start () {
    10.  
    11.         coroutine = TurnON();
    12.         mask = GetComponent<Mask>();
    13.     }
    14.  
    15.     IEnumerator TurnON()
    16.     {
    17.         while (true)
    18.         {
    19.             Hide_Grtaphics();
    20.             yield return new WaitForSeconds(0.5f);
    21.                 Show_Grtaphics();
    22.                     yield return new WaitForSeconds(0.5f);
    23.         }
    24.     }
    25.  
    26.     void Update () {
    27.  
    28.         if (Input.GetKeyDown(key))
    29.         {
    30.             keyPressed = !keyPressed;
    31.             if (keyPressed)
    32.                 StartCoroutine(coroutine);
    33.             else
    34.             {
    35.                 StopCoroutine(coroutine);
    36.                 Show_Grtaphics();
    37.             }
    38.         }
    39.     }
    40.  
    41.     private  void Show_Grtaphics()
    42.     {
    43.         mask.showMaskGraphic = true;
    44.     }
    45.  
    46.     private  void Hide_Grtaphics()
    47.     {
    48.         mask.showMaskGraphic = false;
    49.     }
     
  3. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    I believe there is a car available in the standard assets package- it doesn't have indicators, but I think it does have effects and lights that turn on and off with input- you could look there to find out how they did it.
     
  4. Smagacz

    Smagacz

    Joined:
    Feb 10, 2016
    Posts:
    23
    thanks