- Home /
Invoke Repeating not incrementing a resource
Hi all,
I've run into a snag with one of my scripts. This script is supposed to increment a resource by a certain number every so often and then stop when the resource limit has been reached.
The script is attached to a building that when constructed will activate the attached script. I attached a debug log that said it had incremented the resource once and then was never heard from again. It also didn't add the desired resource to the pool.
using UnityEngine;
using System.Collections;
public class resourceTick : MonoBehaviour {
private resourceGUI material;
public int incrementBy = 2;
public float prodTime = 1f;
void Start(){
InvokeRepeating("resourceGain", 1, prodTime);
}
void resourceGain(){
material = GameObject.Find ("Player").GetComponent<resourceGUI>();
int maximumMetal = material.maxMetal;
material = GameObject.Find("Player").GetComponent<resourceGUI>();
int curMetal = material.currentMetal;
if (curMetal < maximumMetal){
curMetal += incrementBy;
Debug.Log("Creating Metal: " + curMetal);
}
if(curMetal >= maximumMetal){
curMetal = maximumMetal;
CancelInvoke();
}
}
}
I'm new to programming so I no doubt did something out of order.
Any help is greatly appreciated.
I think this is probably because you are using void Start () but I'm no expert on Invoke.
Answer by Anxo · Sep 20, 2014 at 11:47 PM
I would check 2 things,
put a debug in the clause that cancels the invoke or simply comment the cancel out and see if the invoke repeats then.
Check you have overwritten the public variable for repeat time in the inspector. So you may thing its 1 but it may be 100 in the inspector.
Damit, wrote another answer but can't answer twice and it took a while to write! Try this, clean for errors firs as I am not using unity atm but the logic should work.
void OnEnable(){
StartCoroutine("GeneradeGoods");
}
void OnDisable(){
StopCoroutine("GeneradeGoods");
}
IEnumirator GeneradeGoods(){
material = GameObject.Find ("Player").GetComponent<resourceGUI>();
int maximum$$anonymous$$etal = material.max$$anonymous$$etal;
int cur$$anonymous$$etal = material.current$$anonymous$$etal;
while (true){
yield return new WaitForSeconds(prodTime);
while(cur$$anonymous$$etal >= maximum$$anonymous$$etal) yield return null;
cur$$anonymous$$etal += incrementBy;
}
}
Hi Anxo, I had thought about using a coroutine but I'm still just going through some of the tutorials about them. Do you $$anonymous$$d explaining a little about how the above works?
Hi $$anonymous$$rJCSmith, placing debug.log in the right places should help you find the issue but I think Eric's answer might be your problem.
The Coroutine I wrote works like this,
set material, declare and set maximum$$anonymous$$etal declare and set cur$$anonymous$$etal while true(true is always true, so while always) loop through the following code. wait for a prodTime before moving on to the next line. if curent metal is more than or = to maxmetal, wait till that is no longer the case. Then move on to the next line, cur$$anonymous$$etal+= incrementBy (bc we loop indefinatly...)Go back up to wait for prodTime, and follow instructions under the while(true)
Hi Anxo, thank you for your help on this one and taking the time to explain how the coroutine works. Appreciate it!
Answer by Eric5h5 · Sep 21, 2014 at 12:55 AM
You declare a local variable called "curMetal" but don't really do anything with it. I don't know where the resource is stored, but wherever it is, it's untouched and will never increase.
Hi Eric, thank you for replying. Is the first 'if' statement not using the ints referenced above it?
I had set up cur$$anonymous$$etal to equal current metal in my GUI script. Did I call the variable incorrectly?
What Eric is saying here is that your resourcesGain $$anonymous$$ethod creates a local variable, int cur$$anonymous$$etal = material.current$$anonymous$$etal;
and later you change that variable's value to cur$$anonymous$$etal += incrementBy;
But you never do anything with the variable, you just create it. You do not have something like, material.current$$anonymous$$etal = cur$$anonymous$$etal ;
Hi Anxo and Eric. It wouldn't let me mark both answers as correct but you both gave excellent advice. It was also correct in that I wasn't correctly setting what to increment. Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using invokerepeatng in update 4 Answers
How do I replace Invoke Repeating to allow a repeat rate change 1 Answer