Remove an object from a list and add another other obejct?
Okay, so not a very great question, but let me give some context. In my game, I have 6 weapons of different types (SMG, pistol, sniper, etc.) and each type of weapon has 5 rarity levels. I have weapon switching already successful, and the ability to add the weapons to the list on pickup already. What I would like to be able to do is when the player picks up a weapon, to check if the player either a) already has the same weapon, or b) has the same type of weapon but different rarity level. I have been able successfully make it so the player cannot pick up weapons of the same type but lower rarity than the player already has, but what I would like to happen is when the player picks up a higher tier weapon of one they already have, the lower-tier weapon will be removed from the list of guns the player has and the lower-tier gun's gameObject be destroyed, and the higher-tier weapon added to the list and instantiated as the player's current gun, so that the player doesn't have to cycle through 30 different weapons to find the one they're looking for, as well as they can only have one weapon of each type. (I was also thinking of doing an inventory system instead of this, but I already had most of the logic in place for this so I went with this.) Here is the code that runs when the gun pickup object OnTriggerEnter2D is called and the player is the one triggering it: Variable clarifications: "theGun" is the Gun object attached to the pickup (so which Gun the pickup will give the player); "availableGuns" is a List of guns the player already currently has
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" && waitToBeCollected <= 0)
{
bool hasGun = false;
int gunToRemove = 0;
foreach(Gun gunToCheck in PlayerController.instance.availableGuns)
{
if(theGun.weaponName == gunToCheck.weaponName)
{
hasGun = true;
}
if(theGun.weaponType == gunToCheck.weaponType)
{
if (theGun.rarityLevel <= gunToCheck.rarityLevel)
{
hasGun = true;
theGun.isBetter = false;
}
else
{
hasGun = false;
theGun.isBetter = true;
gunToRemove = PlayerController.instance.availableGuns.IndexOf(gunToCheck);
}
}
}
if (!hasGun)
{
if (theGun.isBetter)
{
PlayerController.instance.availableGuns.Remove(PlayerController.instance.availableGuns[gunToRemove]);
Destroy(PlayerController.instance.availableGuns[gunToRemove].gameObject);
}
Gun gunClone = Instantiate(theGun);
gunClone.transform.parent = PlayerController.instance.gunArm;
gunClone.transform.position = PlayerController.instance.gunArm.position;
gunClone.transform.localRotation = Quaternion.Euler(Vector3.zero);
gunClone.transform.localScale = Vector3.one;
PlayerController.instance.availableGuns.Add(gunClone);
PlayerController.instance.currentGun = PlayerController.instance.availableGuns.Count - 1;
PlayerController.instance.SwitchGun();
Destroy(gameObject);
AudioManager.instance.PlaySFX(24);
}
}
}
*Post bumped because of inactivity
The problem that is happening (which I forgot to mention) is that when I pick up the higher tier weapon, it will delete the lower-tier one from the list, but the element is still there and is now null, which messes up the gun switching system, and the highertier one doesn't get added properly, and the gameobject of the lowertier one doesn't get destroyed in the hierarchy.
Your answer
Follow this Question
Related Questions
Get object to check list for number of specific items 0 Answers
Using foreach to remove and delete bullet in List - C# 3 Answers
Problem in Destroy GameObject on OnTriggerEnter2D 0 Answers
I can't figure out why a game object isn't being destroyed. 0 Answers
Compare GameObject with an array 1 Answer