I’m making a reload system similar on escape from tarkov, but after a week of experimenting, debugging etc i’m still struggling to make it work. I’m new in game dev in fact this is my 1st project
this is my logic:
i have this script attached to my magazine prefab so that weapons ammo depends on the magazine ammo count.
public class MagazineAmmo : MonoBehaviour
{
public int magAmmo = 12;
public int currentAmmo;
void Start()
{
currentAmmo = magAmmo;
}
}
i also have vest/bulletproof vest prefab with magazine pouches that has magazines prefab in it as a child, this vest is instantiated on player’s body.
now i want to access those magazines with higher magAmmo so i could transfer that to the weapon, this is what i’ve done so far
public class MagazineOnVest : MonoBehaviour
{
public string searchTag;
public List<GameObject> rifleMags = new List<GameObject>();
public List<MagazineAmmo> magAmmoCount = new List<MagazineAmmo>();
public List<int> ammoCount = new List<int>();
void Start()
{
}
void Update()
{
if (searchTag != null)
{
FindObjectwithTag(searchTag);
}
}
public void FindObjectwithTag(string _tag)
{
rifleMags.Clear();
magAmmoCount.Clear();
ammoCount.Clear();
Transform parent = transform;
GetChildObject(parent, _tag);
}
public void GetChildObject(Transform parent, string _tag)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
MagazineAmmo mags = child.GetComponent<MagazineAmmo>();
if (child.tag == _tag)
{
rifleMags.Add(child.gameObject);
magAmmoCount.Add(mags);
ammoCount.Add(mags.magAmmo);
if (ammoCount != null)
{
ammoCount.Sort();
}
}
if (child.childCount > 0)
{
GetChildObject(child, _tag);
}
}
}
this script is attached on vest prefab, getting the magazine with higher ammo is where i am currently struggeling.
if anyone has a better logic for this pls help me, i am really out of ideas thanks…