InvokeRepeating

Hi I’m using c# and I’m currently making a perk script and when the player picks up this perk the players ammo increases every 10 seconds. The perk is a game object as well.However my code doesn’t seem to be right as when I play through the game and error comes up saying 59838-capture23.png

Here is the script I have used;
public class PerkScript : MonoBehaviour {

    public GameObject ammoPerk;
    float ammoUpTime = 10f;

	// Use this for initialization
	void Start () {
        InvokeRepeating("TopUpAmmo", ammoUpTime, ammoUpTime);
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        PlayerScript playerScript = GetComponent<PlayerScript>();
        if (hit.gameObject.tag == "AmmoPerk")
        {
            playerScript.reserveAmmo += 10;
            Destroy(hit.gameObject);
        }
    }
}

Thanks for the help if any and would be much appreciated.

So you’re trying to invoke a ‘TopUpAmmo’ method, but you never actually made the method to call. Also, you want this to run after picking up an object, but you’re calling it from Start.

You need to move the InvokeRepeating to inside the collision detection code, and make the method itself.