Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bybocek · Apr 02, 2017 at 10:03 AM · buttonbutton trigger eventsinventory systembuttonstates

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

How to make the button respond to touch and hold feature? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges