Can someone plz help me with my problem?
Brief description of my problem:- I’m making a fps game for mobile with the help of brackeys tutorials. I have a gun script on each weapon, and a weapon switch script working with the UI button. I have 2 UI buttons for shooting with two events (onpointerdown & onpointerup). These events are connected with the gun script on my primary weapon. The main problem is when I switch he weapon, the primary weapon get disabled and secondary weapon gets enabled, but the shooting buttons are not working with the secondary weapon. If anybody knows better options than this so please please tell me those.
Gun Script I’m using:-
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class gunscript : MonoBehaviour
{
public float gundamage = 2f;
public float gunrange = 100f;
public float firerate = 15f;
public Text ammotext;
public int maxammo = 30;
private int curammo = -1;
public float reloadtime = 2.5f;
private bool isreloading = false;
public Transform maincam;
public ParticleSystem muzzlesystem;
public Animator gunanim;
public Button reloadbut;
private float timetofire = 0f;
bool gunbutpress = false;
bool startreload = false;
bool aimselected = false;
public playermovement walk;
public GameObject crosshair;
public GameObject impacteffect;
public GameObject bulletcase;
public Transform spawnpoint;
// Start is called before the first frame update
void Start()
{
if (curammo == -1)
{
curammo = maxammo;
}
}
// Update is called once per frame
void Update()
{
ammotext.text = curammo.ToString() + "/" + "30";
if (isreloading == true)
{
ammotext.text = "Reloading";
return;
}
if (gunbutpress == true && Time.time >= timetofire)
{
timetofire = Time.time + 1f / firerate;
shoot();
}
if (curammo <= 0)
{
StartCoroutine(reload());
return;
}
}
public void gunbutdown()
{
if (gunbutpress == false)
{
gunbutpress = true;
}
}
public void gunbutup()
{
if (gunbutpress == true)
{
gunbutpress = false;
if (gunanim.GetBool("fire") != false)
{
gunanim.SetBool("fire", false);
}
}
}
public void reloadclicked()
{
if (startreload == false)
{
startreload = true;
}
if (startreload == true && curammo < maxammo)
{
StartCoroutine(reload());
startreload = false;
return;
}
}
public void aim()
{
if (aimselected == false)
{
aimselected = true;
gunanim.SetBool("aim", true);
crosshair.SetActive(false);
}
else
{
aimselected = false;
gunanim.SetBool("aim", false);
crosshair.SetActive(true);
}
}
void shoot()
{
muzzlesystem.Play();
gunanim.SetBool("fire", true);
FindObjectOfType<audiomanage>().play("ak47gun");
curammo--;
RaycastHit hit;
if (Physics.Raycast(maincam.transform.position, maincam.transform.forward, out hit, gunrange))
{ target target = hit.transform.GetComponent<target>();
GameObject shell = Instantiate(bulletcase);
shell.transform.position = spawnpoint.position;
if (target != null)
{
target.takedamage(gundamage);
}
if (target == null)
{
Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
IEnumerator reload()
{
isreloading = true;
FindObjectOfType<audiomanage>().play("ak47reload");
gunanim.SetBool("reloadbool", true);
if (gunbutpress == true)
{
gunanim.SetBool("fire", false);
}
yield return new WaitForSeconds(reloadtime);
isreloading = false;
gunanim.SetBool("reloadbool", false);
curammo = maxammo;
if (gunbutpress == true)
{
gunanim.SetBool("fire", true);
}
}
}