- Home /
I want to change the fire button when I change the weapon.
I want to change the fire button when I change the weapon. Input.GetAxis ("Fire1") There is no problem using it. But I want to use the ui button. When I use uibutton I get the reference from which gun. Uibutton is working only on the reference weapon. The uibutton used by each weapon should be different, but how? How can i do it differently.
FIREBULLET
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class FireBullet : MonoBehaviour {
 public float timeBetweenBullets = 0.15f;
 public GameObject projectile;
 public Slider PlayerAmmoSlider;
 public Button firebutton;
 public int maxRounds;
 public int startingRounds;
 int remainingRounds;
 public Texture buttonImage = null;
 public KeyCode keyCode = KeyCode.None;
 float nextBullet;
 AudioSource gunMuzzleAudioSource;
 public AudioClip shootSound;
 public AudioClip reloadSound;
 public Sprite weaponSprite;
 public Image weaponImage;
 void Awake () {
     firebutton = GetComponent<Button>();
     nextBullet = 0f;
     remainingRounds = startingRounds;
     PlayerAmmoSlider.maxValue = maxRounds;
     PlayerAmmoSlider.value = remainingRounds;
     gunMuzzleAudioSource = GetComponent<AudioSource> ();
 }
 private void Update(){
     PlayerController myPlayer = transform.root.GetComponent < PlayerController> ();
 
     if (Input.GetAxis ("Fire1") > 0 && nextBullet < Time.time && remainingRounds>0) { //Pc Mode
         nextBullet = Time.time + timeBetweenBullets;
         Vector3 rot;
         if (myPlayer.GetFacing () == -1f) {
             rot = new Vector3 (0, -90, 0);
         } else
             rot = new Vector3 (0, 90, 0);
         Instantiate (projectile, transform.position, Quaternion.Euler (rot));
         playASound (shootSound);
         remainingRounds -= 1;
         PlayerAmmoSlider.value = remainingRounds;
     }
 }
 public void reload(){
     remainingRounds = maxRounds;
     PlayerAmmoSlider.value = remainingRounds;
     playASound (reloadSound);
 }
 void playASound (AudioClip playTheSound){
     gunMuzzleAudioSource.clip = playTheSound;
     gunMuzzleAudioSource.Play ();
 }
 public void initializeWeapon(){
     gunMuzzleAudioSource.clip = reloadSound;
     gunMuzzleAudioSource.Play ();
     nextBullet = 0;
     PlayerAmmoSlider.maxValue = maxRounds;
     PlayerAmmoSlider.value = remainingRounds;
     weaponImage.sprite = weaponSprite;
 }
}
INVENTORY MANAGER
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class InventoryManager : MonoBehaviour {
 public GameObject[] weapons;
 bool[] weaponAvailable;
 public Image weaponImage;
 bool change;
 int currentWeapon;
 Animator weaponImageAnim;
 void Start () {
     weaponAvailable = new bool [weapons.Length];
     for(int i= 0; i < weapons.Length; i++) weaponAvailable[i] = false;
     currentWeapon = 0;
     weaponAvailable [currentWeapon] = true;
     //for(int i= 0; i < weapons.Length; i++) weaponAvailable[i] = true;
     weaponImageAnim = weaponImage.GetComponent<Animator> ();
     deactivateWeapons ();
     setWeaponActive (currentWeapon);
 }
 public void ButtonClick() {
         int i;
         for (i = currentWeapon + 1; i < weapons.Length; i++) {
             if (weaponAvailable [i] == true) {
                 currentWeapon = i;
                 setWeaponActive (currentWeapon);
                 return;
             }
         }
         for (i = 0; i < currentWeapon; i++) {
             if (weaponAvailable [i] == true) {
                 currentWeapon = i;
                 setWeaponActive (currentWeapon);
                 return;
             }
         }
     
 }
 public void setWeaponActive(int whichWeapon){
     if (!weaponAvailable [whichWeapon])return;
     deactivateWeapons ();
     weapons [whichWeapon].SetActive (true);
     weapons [whichWeapon].GetComponentInChildren<FireBullet> ().initializeWeapon ();
     weaponImageAnim.SetTrigger ("weaponSwitch");
 }
 void deactivateWeapons(){
     for (int i = 0; i < weapons.Length; i++) weapons [i].SetActive (false);
 }
 public void activateWeapon(int whichWeapon){
     weaponAvailable [whichWeapon] = true;
 }
     
}
Your answer
 
 
             Follow this Question
Related Questions
How to create simple stage systems? 1 Answer
Highlighted buttons stay highlighted when I deactivate a canvas. No way to unhighlight them. 1 Answer
how to insert hover effect on unity button and active color button when the button is being click ? 0 Answers
UI button mobile - want a button drag to register as a click 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                