- Home /
Bullet pickups/Accessing the inactive objects
How to pick up bullets more efficiently? I have a bullet pickup script with a string and object with shooting script. Everything occurs in shooting script: shooting raycasts, bullet count(each weapon has it's own magazine size and bullet count). But I can't increase it from the bullet pickup script because each time I change weapon, every other weapon is disabled. E.g the pickup is meant meant for a pistol but I enabled rifle for now. I go to pickup loop through the weapons(I CANT LOOP THROUGH INACTIVE OBJECTS AND IT GIVES ME ERROR) How can I iterate through inactive objects? Or how else to manage bullets?
public class BulletPickup : MonoBehaviour
{
public int bulletAmount = 10;
public string weaponForPickup;
GameObject weaponRoot;
private void Start()
{
weaponRoot = GameObject.FindGameObjectWithTag("WeaponRoot");
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
foreach (Transform weapon in weaponRoot.transform)
{
if (weapon.gameObject.GetComponent<ShootingScritpt>().GetWeaponType() == weaponForPickup)
{
weapon.gameObject.GetComponent<ShootingScritpt>().AddFoundBullets(bulletAmount);
}
}
Destroy(gameObject);
}
}
}
Answer by conguerror · May 06, 2020 at 05:21 PM
modify your handling class to point to a current weapon and have the other weapons remain enabled but not receiving updates. This would decouple your actual weapons from unity gameobjects. This is also a good basis for a data driven approach to your weapon system. Or try to do something with OnEnabled(): https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html
Your answer
Follow this Question
Related Questions
Photon - Change others player health problem 0 Answers
How to add Muzzle Flash to Weapon 1 Answer
FireWeapon.cs not working 2 Answers
Collider.ClosestPointOnBounds Performance? 0 Answers
Any problem with using real weapons in a FPS game? 0 Answers