How do I stop this script?
I have this script which is attached to a UI button. It controls shooting and ammo. I still need to refactor it, but it works except for this one thing. I am trying to get the script to stop running, but when I put this.enabled = false; the character shoots continuously without any stop. I am using Unity 2d and it is a top-down shooter. Please help.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
public class myAutoFire : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { [SerializeField] private Ammo ammoController; [SerializeField] private int ammoLowered = 1; public Transform firePoint; public GameObject bulletPrefab; public float bulletForce = 20f; public int playerAmmo; [SerializeField] private Text ammoText; public GameObject point;
void Start()
{
UpdateAmmo();
}
public void OnPointerDown(PointerEventData eventdata)
{
InvokeRepeating ("FireMe", 0, 0.1f);
}
public void OnPointerUp(PointerEventData eventdata)
{
CancelInvoke ("FireMe");
}
void FireMe()
{
//Debug.Log ("firing "+Time.time);
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
playerAmmo -= ammoLowered;
UpdateAmmo();
}
void UpdateAmmo()
{
ammoText.text = playerAmmo.ToString("0");
if(playerAmmo <= 0)
{
print("Out of Ammo");
//FireMe.enabled = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613015233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Prefab Direction - shooting 2 Answers
2d shooting,Shooting bullets 2d 0 Answers
Unity2D Top-Down Simple Shooting Script C# 1 Answer
MissingReferenceException what sshould I do? 0 Answers
[Unity2D] Adding Sound to a Prefab 0 Answers