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 would check 2 things,

  1. put a debug in the clause that cancels the invoke or simply comment the cancel out and see if the invoke repeats then.

  2. 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.

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.