Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

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

Discussion in 'Scripting' started by StealthBeast3xx0, Aug 17, 2020.

  1. StealthBeast3xx0

    StealthBeast3xx0

    Joined:
    Mar 29, 2020
    Posts:
    6
    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:-
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class gunscript : MonoBehaviour
    6. {
    7.     public float gundamage = 2f;
    8.     public float gunrange = 100f;
    9.     public float firerate = 15f;
    10.     public Text ammotext;
    11.     public int maxammo = 30;
    12.     private int curammo = -1;
    13.     public float reloadtime = 2.5f;
    14.     private bool isreloading = false;
    15.     public Transform maincam;
    16.     public ParticleSystem muzzlesystem;
    17.     public Animator gunanim;
    18.     public Button reloadbut;
    19.     private float timetofire = 0f;
    20.     bool gunbutpress = false;
    21.     bool startreload = false;
    22.     bool aimselected = false;
    23.     public playermovement walk;
    24.     public GameObject crosshair;
    25.     public GameObject impacteffect;
    26.     public GameObject bulletcase;
    27.     public Transform spawnpoint;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.        
    33.         if (curammo == -1)
    34.         {
    35.             curammo = maxammo;
    36.         }
    37.        
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         ammotext.text = curammo.ToString() + "/" + "30";
    44.         if (isreloading == true)
    45.         {
    46.             ammotext.text = "Reloading";
    47.             return;
    48.         }
    49.         if (gunbutpress == true && Time.time >= timetofire)
    50.         {
    51.             timetofire = Time.time + 1f / firerate;
    52.             shoot();
    53.         }
    54.         if (curammo <= 0)
    55.         {
    56.             StartCoroutine(reload());
    57.             return;
    58.         }
    59.        
    60.     }
    61.     public void gunbutdown()
    62.     {
    63.         if (gunbutpress == false)
    64.         {
    65.             gunbutpress = true;
    66.            
    67.         }
    68.     }
    69.     public void gunbutup()
    70.     {
    71.         if (gunbutpress == true)
    72.         {
    73.             gunbutpress = false;
    74.             if (gunanim.GetBool("fire") != false)
    75.             {
    76.                 gunanim.SetBool("fire", false);
    77.             }
    78.            
    79.         }
    80.     }
    81.    
    82.     public void reloadclicked()
    83.     {
    84.         if (startreload == false)
    85.         {
    86.             startreload = true;
    87.         }
    88.  
    89.         if (startreload == true && curammo < maxammo)
    90.         {
    91.             StartCoroutine(reload());
    92.             startreload = false;
    93.             return;
    94.         }
    95.     }
    96.     public void aim()
    97.     {
    98.         if (aimselected == false)
    99.         {
    100.             aimselected = true;
    101.             gunanim.SetBool("aim", true);
    102.             crosshair.SetActive(false);
    103.         }
    104.         else
    105.         {
    106.             aimselected = false;
    107.             gunanim.SetBool("aim", false);
    108.             crosshair.SetActive(true);
    109.         }
    110.     }
    111.     void shoot()
    112.     {
    113.         muzzlesystem.Play();
    114.         gunanim.SetBool("fire", true);
    115.         FindObjectOfType<audiomanage>().play("ak47gun");
    116.         curammo--;
    117.         RaycastHit hit;
    118.         if (Physics.Raycast(maincam.transform.position, maincam.transform.forward, out hit, gunrange))
    119.         {   target target = hit.transform.GetComponent<target>();
    120.            GameObject shell = Instantiate(bulletcase);
    121.             shell.transform.position = spawnpoint.position;
    122.             if (target != null)
    123.             {
    124.                 target.takedamage(gundamage);
    125.             }
    126.             if (target == null)
    127.             {
    128.                 Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
    129.             }
    130.         }
    131.     }
    132.     IEnumerator reload()
    133.     {
    134.         isreloading = true;
    135.         FindObjectOfType<audiomanage>().play("ak47reload");
    136.         gunanim.SetBool("reloadbool", true);
    137.  
    138.  
    139.         if (gunbutpress == true)
    140.         {
    141.             gunanim.SetBool("fire", false);
    142.         }
    143.  
    144.         yield return new WaitForSeconds(reloadtime);
    145.  
    146.         isreloading = false;
    147.         gunanim.SetBool("reloadbool", false);
    148.         curammo = maxammo;
    149.         if (gunbutpress == true)
    150.         {
    151.             gunanim.SetBool("fire", true);
    152.         }
    153.     }
    154. }
    155.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,421
    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.
     
  3. StealthBeast3xx0

    StealthBeast3xx0

    Joined:
    Mar 29, 2020
    Posts:
    6
    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*