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

How do I make something happen every 3 or any amout of seconds?

Discussion in 'Scripting' started by Brafedr, Sep 4, 2020.

  1. Brafedr

    Brafedr

    Joined:
    Aug 30, 2020
    Posts:
    7
    i am a begginer and i need help on this
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
  3. Brafedr

    Brafedr

    Joined:
    Aug 30, 2020
    Posts:
    7
    It did not work maybe if i show my script to you then you might be able to fix it.
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    if time % 3 == 0
     
  5. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Yes give it a try.
     
  6. Brafedr

    Brafedr

    Joined:
    Aug 30, 2020
    Posts:
    7
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class EnemySpawner : MonoBehaviour
    5. {
    6.     public float random = 1f;
    7.     public GameObject enemyPrefab;
    8.     void Start()
    9.     {
    10.         InvokeRepeating("Update", 3.0f, 100000f);
    11.  
    12.     }
    13.  
    14.  
    15.  
    16.     void Update()
    17.     {
    18.         if (random <= 1)
    19.         {
    20.            
    21.             Instantiate(enemyPrefab, transform.position, Quaternion.identity);
    22.         }
    23.     }
    24. }
    25.  
     
  7. Brafedr

    Brafedr

    Joined:
    Aug 30, 2020
    Posts:
    7
    I sent the code
     
  8. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Dont use the name Update here for the Spawn method name because this is a special monobehavior method. What the float random means?

    This code spawn a new enemy each 3 seconds (but at same position):
    Code (CSharp):
    1. using UnityEngine;
    2. public class EnemySpawner : MonoBehaviour
    3. {
    4.     public float random = 1f;
    5.     public GameObject enemyPrefab;
    6.  
    7.     void Start()
    8.     {
    9.         // Call the method Spawn every 3 seconds
    10.         InvokeRepeating("Spawn", 3.0f, 3.0f);
    11.     }
    12.  
    13.     void Spawn()
    14.     {
    15.       Instantiate(enemyPrefab, transform.position, Quaternion.identity);
    16.     }
    17.  
    18.    void Update()
    19.    {
    20.       // Update is called every frame, if the MonoBehaviour is enabled.
    21.       // Update is the most commonly used function to implement any kind of game script.
    22.       // Not every MonoBehaviour script needs Update.
    23.    }
    24. }
     
    Last edited: Sep 4, 2020
    Brafedr likes this.
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    That will not work at all. The game could easily jump from 2.97 seconds straight to 3.03 seconds or something like that, and then this condition wouldn't trigger.
     
    PraetorBlue likes this.
  10. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    time not Time.*
    you increment elsewhere manually for more control
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I'm not sure exactly what you have in mind, but every option I can think of either still has the original issue or could be simplified by eliminating the "if (time % 3 == 0)" part.

    The normal way to do something every 3 seconds with an accumulator variable would be something like:
    Code (CSharp):
    1. void Update()
    2. {
    3.     accumulatedTime += Time.deltaTime;
    4.     while (accumulatedTime >= timeBetweenEvents)
    5.     {
    6.         accumulatedTime -= timeBetweenEvents;
    7.         DoEvent();
    8.     }
    9. }
    (With a few possible minor variations depending on how you want to handle the "overshoot", none of which involve a % operator.)

    Testing "if (time % 3 == 0)" really cannot possibly be a good idea unless "time" is an int, rather than a float (not so much because of the % part, but the == part; you should almost never test floats for exact equality). So you might do something like that in a turn-based game where "time" increments by 1 each turn. But if the thing you care about is elapsed seconds (not game turns), I really can't think of any sensible scenario where your time variable would be an int.
     
    neoshaman and mopthrow like this.