How to set onpointerdown & onpointerup events through script for android in unity?

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);
        }
    }
}

I don’t see any switch code in there, but if it works on one, it should work on the other weapon.

I recommend liberally sprinkling Debug.Log() statements through your code (especially the switch weapon code) in order to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

You should also shortcut in some keypresses so you can test it directly in the editor for functionality, and not have to go back and forth to android. Once it works in editor, then test on android.

The weapon switch script is separate from the gun script used by each weapon. And events of onpointerup & onpointerdown are associated with gun script. So whenever I change the gun, the event on the UI shooting buttons are not changing as their events are associated with the previous weapon.

do you know any other methods of weapon switch for android?

Sorry for bad English