Invoke after every loop of "if" statement?
So I have a very basic script to test the increment of an integer called "wood". I have a method that contains an if loop that increments wood by 1 if it's less than 10. I want wood to increment every 2 seconds so I set an invoke in the Update method. The problem is, after the first two seconds, the wood is fully incremented instantly instead of over time. My script is this:
using UnityEngine;
using System.Collections;
public class AddWood : MonoBehaviour {
public int wood = 1;
void IncreaseWood() {
if (wood < 10) {
wood++;
print (wood);
}
}
void Update() {
Invoke ("IncreaseWood", 2);
}
}
Comment
Best Answer
Answer by Happy-Zomby · Apr 11, 2016 at 07:46 AM
Hi, you should use invoke repeating http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html InvokeRepeating("IncreaseWood", 0, 2.0F);
Your answer
![](https://koobas.hobune.stream/wayback/20220612074604im_/https://answers.unity.com/themes/thub/images/avi.jpg)