I have a method that takes the amount of time that has passed from the Timer script that I have and decrease the spawn time by seconds. But when I run, the float does not decrease.
My spawn code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cubes : MonoBehaviour {
public float delay = 2f;
public GameObject cubes;
public Timer timer;
// Use this for initialization
void Start () {
InvokeRepeating ("Spawn", delay, delay);
GameObject time = GameObject.Find("Timer");
timer = time.GetComponent<Timer>();
}
// Update is called once per frame
void Spawn () {
Instantiate(cubes, new Vector3 (Random.Range(-6, 6),10,0),Quaternion.identity);
}
void Update ()
{
if (timer.timer % 10 == 0) {
MinusDelay ();
}
}
public void MinusDelay () {
if (delay <= 0.1f) {
delay -= 0.1f;
}
}
}
Timer code (The Cubes script is attached to the MainCamera)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public float seconds = 0.0f;
public float timer = 0.0f;
public int minuteCount= 0;
public int hourCount = 0;
public Text text;
public GameObject delayer;
// Use this for initialization
void Start () {
text = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
seconds += Time.deltaTime;
text.text = hourCount + ":" + minuteCount + ":" + seconds;
if (seconds >= 60) {
minuteCount++;
seconds = 0;
}
else if (minuteCount >= 60) {
hourCount++;
minuteCount = 0;
}
}
void DeActivate () {
gameObject.SetActive (false);
}
}
Hi, I’m a beginner at Unity, but I might be able to help.
I can see 2 problems in your Cubes script:
In your Update method, you do if (timer.timer % 10 == 0), the timer variable is a float and the chances that it gets to an exact multiple of 10 are very slim. You probably get 10.1 or 30.0002. To fix that, you could use Mathf.ApproximatlyEquals(timer.timer % 10, 0, SOME_VALUE), I don’t know the exact function name. There is a parameter that is used to choose how close the 2 numbers need to be to be considered the same. Make it too small and you risk jumping over and not calling MinusDelay(). Make it too big and you risk calling MinusDelay() multiple times in a row. You may want instead to change the type of Timer.seconds to an int and use that in Cubes.Update(). then set seconds = (int)timer; in Timer.Update() instead of seconds += Time.deltaTime.
In C#, when passing a value type variable to a function, it’s value is copied to the function and modification to the original are not taken into account (not updated) when in the function, here InvokeRepeating(…). A value type is an int, a float, double, string or any struct. Reference types are classes. So if you call InvokeRepeating (“Spawn”, delay, delay) with delay = 2, then it repeat at 2 seconds interval forever, even if you change delay to 0.5. Also be careful to not call Invoke and InvokeRepeating with negative values or zero. You could control spawning all in Cubes.Update(), but that’s boring or you could look into coroutines. Replace InvokeRepeating ("Spawn", delay, delay); with StartCoroutine(SpawnCoroutine()); Then add this method:
private IEnumerator SpawnCoroutine()
{
yield return new WaitForSeconds(delay);
while(keepOnSpawning)
{
Spawn();
yield return new WaitForSeconds(delay);
}