Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by MichaelABC · Apr 10 at 02:35 PM · memorylists

Remove Object from a list if it is already in another list?

alt text

This part of script allow me to attached these "molecules" gameobjects to the player, so to manage them I store them in the list ObjectGrabed.

 void ALL_Attach(Vector2 pos, Transform _holder)
     {
  
         RaycastHit2D Ext2Int = Physics2D.Raycast(pos, Vector2.zero, 0);
  
         if (Ext2Int.collider != null && Ext2Int.collider.tag == "Ext_Molecule")
         {
             Ext2Int.collider.transform.parent = _holder;
             Ext2Int.collider.gameObject.transform.position = _holder.position;
             Ext2Int.collider.gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
             Ext2Int.collider.gameObject.tag = "Int_Molecule";
             Ext2Int.collider.gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
            
             objectGrabed.Add(Ext2Int.collider.gameObject);
             Debug.Log("ObjectGrabed");
  
             CursorXhair.Explosives.Remove(Ext2Int.collider.gameObject);
             Debug.Log("Explosive removed");
         }
     }



While this part of another script allows me to "eject" the molecules which can then be detonated remotely. So I add them to a different list of Explosives and remove them from ObjectGrabed. However this explosive property, which is defined in another script only for the objects inside the Explosives list, should not be kept if I decide to attach the molecule again. That's why in the script above I try to remove the objects from the Explosive list. I should note that all debuglogs return but the "Explosives removed", so what am I doing wrong? Is there a better way to do what I am trying to do? I think the reference in the method to remove the explosive must be wrong, but I don't know how else to write it.

 void Eject()
     {
         if (Player.objectGrabed.Count >= 1 && Input.GetKeyDown(KeyCode.Mouse0))
         {
             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             var closestObject = collidingObjects.OrderBy(_ => (_.transform.position - (Vector3)mousePos).sqrMagnitude).First();
  
             Transform[] MoleculeChildren = closestObject.GetComponentsInChildren<Transform>();
             foreach (Transform item in MoleculeChildren)
             {
                 item.tag = "Mid_Molecule";
             }
  
             closestObject.tag = "Mid_Molecule";
             closestObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
             closestObject.GetComponent<Rigidbody2D>().AddForce(closestObject.transform.parent.up * Player.throwSpeed);
             closestObject.transform.SetParent(null);
             StartCoroutine(ChangeTag());
            
             if (Player.objectGrabed.Count >= 1)
             {
                 Explosives.Add(Player.objectGrabed.Last());
                 Debug.Log("Explosive Added");
             }
             Player.objectGrabed.RemoveAt(Player.objectGrabed.Count - 1);
             Debug.Log("ObjectGrabed removed");
  
         }
     }
     IEnumerator ChangeTag()
     {
      
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         var closestObject = collidingObjects.OrderBy(_ => (_.transform.position - (Vector3)mousePos).sqrMagnitude).First();
         yield return new WaitForSeconds(3f);
         Transform[] MoleculeChildren = closestObject.GetComponentsInChildren<Transform>();
         foreach (Transform item in MoleculeChildren)
         {
             item.tag = "Ext_Molecule";
         }
         closestObject.tag = "Ext_Molecule";      
     }


example.png (4.2 kB)
Comment
Add comment
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

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

137 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

Related Questions

Performance and memory 1 Answer

WP8 Profiler Memory Discrepancy 0 Answers

When is a referenced texture actually loaded? 0 Answers

Unity memory thirst 0 Answers

Deleting www.texture from memory 2 Answers


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