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

Enable button after a certain amount of time in c#

Discussion in 'Scripting' started by Deleted User, Nov 29, 2016.

  1. Deleted User

    Deleted User

    Guest

    I want to create a c# script that will activate a button automatically after a certain amount of time. But after you press it it will diactivate the button automatically again nad wait until the time passed again.
    Thank you in advance.
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This is a generic script to add a timer to anything, you can add this to any gameObject
    Just set the timeToWait in the inspector. Add your own code to TimerFinished. The Public Function ResetTimer is there to startt the timer again if needed.
    Code (CSharp):
    1. public class WaitTimer : MonoBehaviour
    2. {
    3.          public float timeToWait;
    4.          private currentWaitTime;
    5.          private bool checkTime;
    6.  
    7.          void Awake()
    8.          {
    9.                   ResetTimer();
    10.          }
    11.          void Update()
    12.          {
    13.                  if (checkTime)
    14.                  {
    15.                          currentWaitTime -= Time.deltaTime;
    16.                          if (currentWaitTime < 0)
    17.                          {
    18.                                 TimerFinished();
    19.                                 checkTime = false;
    20.                           }
    21.                   }
    22.            }
    23.  
    24.            public void ResetTimer()
    25.            {
    26.                   currentWaitTime = timeToWait;
    27.                    checkTime = true;
    28.            }
    29.            void TimerFinished()
    30.            {
    31.                  // Add Code here for timer finishing
    32.                  // Like activating a button
    33.             }
    34. }
     
  3. Deleted User

    Deleted User

    Guest

    It says that currentWaitTime does not exist in your project. what should i do?
     
  4. scionwest

    scionwest

    Joined:
    Dec 19, 2009
    Posts:
    26
    Couldn't you do this with a coroutine as well?

    Code (CSharp):
    1. void Start()
    2. {
    3.     StartCoroutine(ButtonCoroutine());
    4. }
    5.  
    6. IEnumerator ButtonCoroutine()
    7. {
    8.     yield return new WaitForSeconds(3f);
    9.     // Turn on the button & make it active
    10. }
    11.  
    Something along these lines?
     
    Last edited: Nov 29, 2016
    LiterallyJeff likes this.
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    This is the simplest way in my opinion. You also don't need that final "yield return null". As long as the code will hit at least one yield statement the coroutine is valid.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    If you throw CallLater.cs into your project, then you could do it this way:

    Code (csharp):
    1. CallLater.DoAfter(3,  x => {
    2.     myButton.SetActive(true);   // or whatever you want to do
    3. });
    The "3" above means 3 seconds; obviously change that to whatever you like, and put whatever code you need to enable the button in place of line 2.
     
    LiterallyJeff likes this.
  7. scionwest

    scionwest

    Joined:
    Dec 19, 2009
    Posts:
    26
    Edited my answer to remove it, thanks. I'm still new so wasn't sure - just based it off an example I had seen in the docs the other day. Good tip!