Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Repeat an If Statement

Discussion in 'Scripting' started by Mr-Snape, Mar 23, 2019.

  1. Mr-Snape

    Mr-Snape

    Joined:
    Aug 14, 2018
    Posts:
    2
    Hello,

    I'm a beginner and I need help.

    Code (CSharp):
    1. if (currentCash >= shopValue)
    2.         {
    3.             fakeShopButton.SetActive(false);
    4.             realShopButton.SetActive(true);
    5.         }
    How can I check the above code every second? It should be checked again and again whether the current money is high enough.

    Thank you
    Mr Snape
     
  2. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    145
  3. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    63
    *look at my profile pic*
    *it says WRONG*


    It works but is not really perfromant.
    He said, he needs to it to check every second.

    Make an extra function for your check.
    Code (CSharp):
    1. void CheckMoney
    2. {
    3. if (currentCash >= shopValue)
    4.         {
    5.             fakeShopButton.SetActive(false);
    6.             realShopButton.SetActive(true);
    7.         }
    8. }
    and put this in your start function:
    InvokeRepeated("CheckMoney", 0.0f, 1.0f);


    This will call your CheckMoney functoin every 1 second.
    The 0 is there because it needs a parameter to wait before starting.
    If the 0 would be a 5, it would wait 5 seconds and then call it every second.
    But since you want it to start calling every second immediately, just say 0 seconds
     
    Mr-Snape likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,411
    Just use Time. Get current time and subtract by previous time. This way calculate seconds. And I un if check in update. Other alternative is InvokeRepeat.
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,595
    Personally, I would be wary of methods that invoke methods based on strings (such as InvokeRepeating). You lose the benefit of compile time checking. So, in X months/years time, long after you have forgotten there is this InvokeRepeating call going on and you change the target name, the first you will know is at some random runtime point, when the game crashes or bugs out.

    You may also like to consider a coroutine (you could also make
    Start
    be the coroutine if that is all this script does)
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         StartCoroutine(CheckMoney());
    4.     }
    5.  
    6.     IEnumerator CheckMoney()
    7.     {
    8.         while(true)
    9.         {
    10.             yield return m_waitOneSec;
    11.  
    12.             if (currentCash >= shopValue)
    13.             {
    14.                 fakeShopButton.SetActive(false);
    15.                 realShopButton.SetActive(true);
    16.             }
    17.         }
    18.     }
    19.  
    20.     WaitForSeconds m_waitOneSec = new WaitForSeconds(1f);
     
    Mr-Snape likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,411
    Coroutimes are not any easier to debug. People often get into mess using them all over the place.