Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PilgrimFlowers · Jan 02, 2021 at 08:18 AM · triggerdestroyinventorylistsforeach

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PilgrimFlowers · Mar 24, 2020 at 12:10 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges