Currently, I am trying to work on my games shop menu where you can “buy” weapons and then use them. By buying weapons I mean accessing their fire weapon script and setting a small bool called IsActive to true which will then be used by the weapon switch script to determine which weapons it can switch and which weapon it can’t switch.
For now, it only works that way where if a weapon isn’t available it just disables the weapon. I don’t want that. I want to limit it so that way if the weapon isn’t available then the player can’t switch to it until he buys it.
So how do I do it from this code?
public class WeaponSwitching : MonoBehaviour {
public int SelectedWeapon = 0;
// Use this for initialization
void Start () {
SelectWeapon ();
}
// Update is called once per frame
void Update () {
int previousSelectedWeapon = SelectedWeapon;
if (Input.GetKeyDown (KeyCode.Alpha1)) {
SelectedWeapon = 0;
}
if (Input.GetKeyDown (KeyCode.Alpha2) && transform.childCount >= 1) {
SelectedWeapon = 1;
}
if (Input.GetKeyDown (KeyCode.Alpha3) && transform.childCount >= 2) {
SelectedWeapon = 2;
}
if (Input.GetKeyDown (KeyCode.Alpha4) && transform.childCount >= 3) {
SelectedWeapon = 3;
}
if (previousSelectedWeapon != SelectedWeapon) {
SelectWeapon ();
}
}
void SelectWeapon(){
int i = 0;
foreach (Transform weapon in transform) {
if (i == SelectedWeapon && weapon.GetComponent<FireWeapon>().IsActive == true) {
weapon.gameObject.SetActive (true);
weapon.GetComponent<FireWeapon> ().anim.CrossFadeInFixedTime ("Draw", 0.001f);
} else {
weapon.gameObject.SetActive (false);
}
i++;
}
}
}