if(Input.GetKeyDown(KeyCode.G) && ammo <= 9)
{
ableS = false;
while(ammo <= 9)
{
Invoke("bulletplus", 0.5f);
}
ableS = true;
}
This is the main part I am having trouble with.
Down there is ALL my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class gun : MonoBehaviour
{
Vector2 rotation = new Vector2 (0, 0);
public float rot = 3;
public Vector3 pos;
public Rigidbody Orb;
public Text ammoT;
public int ammo = 10;
bool ableS = true;
void Update () {
// Ammo
if(ammo >= 7)
{
ammoT.GetComponent<Text>().color = Color.green;
} else if(ammo >= 4)
{
ammoT.GetComponent<Text>().color = Color.yellow;
} else {
ammoT.GetComponent<Text>().color = Color.red;
}
// Rotation
rotation.x += -Input.GetAxis ("Mouse Y");
rotation.x = Mathf.Clamp(rotation.x, -70, 70);
rotation.y += Input.GetAxis("Mouse X");
transform.eulerAngles = (Vector2)rotation * rot;
pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
// Shoot Choklats
if(Input.GetMouseButtonDown(0) && ammo >= 1 && ableS)
{
ammoT.text = "Ammo: " + ammo.ToString();
ammo -= 1;
Rigidbody projectile = Instantiate(Orb, pos, transform.rotation);
projectile.velocity = transform.forward * 50f;
}
// Reload
if(Input.GetKeyDown(KeyCode.G) && ammo <= 9)
{
ableS = false;
while(ammo <= 9)
{
Invoke("bulletplus", 0.5f);
}
ableS = true;
}
}
void bulletplus()
{
ammo += 1;
ammoT.text = "Ammo: " + ammo.ToString();
}
}

ammo <= 9), yet the body of the loop won't make the condition to be false.Invokecalls the named function at a later time, but the process that calls the named function won't run until theUpdatefunction has finished. Therefore, you are callingInvokeinfinite times. \$\endgroup\$