I’m trying to add weapon swapping in my game, it works but i want to only be able to swap weapons if the weapon has an enabled bool called “isCarrying” on it , this script goes through all of the weapons that are child objects of it and enables and disables them, but i only want them enabled if their own “Shooting” script has the “isCarrying” bool enabled, and if not it just skips over onto the next weapon that does. I hope you can understand what I want, all help is appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSwap : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
//scroll up
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
//scroll down
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount -1;
else
selectedWeapon--;
}
if(selectedWeapon != previousSelectedWeapon)
{
SelectWeapon();
}
}
void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}