Search code examples
c#unity-game-enginecloneinstantiation

How To randomize timer duration between two floats


Currently, This code spawns my prefab with only 1 float aka 1 second or w.e. I'm wanting to spawn my prefab between a minimum float and maximum float but not sure what to do because I'm novice and still learning c# and unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Spawn : MonoBehaviour
{

    [SerializeField]
    public GameObject coin;

    [SerializeField]
    float fTimeIntervals;

    [SerializeField]
    Vector2 v2SpawnPosJitter;

    float fTimer = 0;

    public CurrencyManager cm;


    // Start is called before the first frame update
    void Start()
    {
        fTimer = fTimeIntervals;

    }

    // Update is called once per frame
    void Update()
    {

        fTimer -= Time.deltaTime;
        if (fTimer <= 0)
        {
            fTimer = fTimeIntervals;
            Vector2 v2SpawnPos = transform.position;
            v2SpawnPos += Vector2.right * v2SpawnPosJitter.x * (Random.value - 0.5f);
            v2SpawnPos += Vector2.up * v2SpawnPosJitter.y * (Random.value - 0.5f);
            GameObject cmscript = Instantiate(coin, v2SpawnPos, Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
            cmscript.GetComponent<AutoDestroy>().CM = cm;
        }
    }


}


Solution

  • Split your fTimeIntervals field into a fMaxTimeInterval field and a fMinTimeInterval field then use Random.Range when you reset the timer to set it to a value between those intervals. You can even make a ResetTimer method to do this in Start and Update so if you change how you do it, you only have to change it in one place:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Spawn : MonoBehaviour
    {
    
        [SerializeField]
        public GameObject coin;
    
        [SerializeField]
        float fMinTimeInterval;
    
        [SerializeField]
        float fMaxTimeInterval;
    
        [SerializeField]
        Vector2 v2SpawnPosJitter;
    
        float fTimer = 0;
    
        public CurrencyManager cm;
    
    
        // Start is called before the first frame update
        void Start()
        {
            ResetTimer();
        }
    
        // Update is called once per frame
        void Update()
        {
    
            fTimer -= Time.deltaTime;
            if (fTimer <= 0)
            {
                ResetTimer();
                Vector2 v2SpawnPos = transform.position;
                v2SpawnPos += Vector2.right * v2SpawnPosJitter.x * (Random.value - 0.5f);
                v2SpawnPos += Vector2.up * v2SpawnPosJitter.y * (Random.value - 0.5f);
                GameObject cmscript = Instantiate(coin, v2SpawnPos, Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
                cmscript.GetComponent<AutoDestroy>().CM = cm;
            }
        }
    
        void ResetTimer()
        {
            fTimer = Random.Range(fMinTimeInterval,fMaxTimeInterval);
        }
    }