Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by Dorinbuzilov · Jun 13, 2018 at 01:41 PM · listlistscomparedifferencecontains

Comparing two lists to find the difference

So I am shooting coloured darts at a spinning board. When the dart hits the board it will perform a check using Physics2D.OverlapCircleAll for any nearby darts which are of the same colour. If there are any detected it will add them to a list called Links. I want to be able to compare the other dart's Link list with the current dart and if there is any differences to be able to add that to the current list.

 ![public class DartOptimized : MonoBehaviour
 {
 
     // Speed of the Dart
     public float Velocity;
 
     // Board transform
     Transform boardTransform;
     // This dart transform
     Transform thisDartTransform;
     //This dart layer
     LayerMask thisLayer;
 
     // Stopping distance for dart
     public float boardContact;
 
     // Distance to check for surrounding darts once contact is made with the board
     public float checkRadius;
 
     // Links list
     public List<GameObject> Links = new List<GameObject>();
     // Material for line 
     public Material LineRenderMat;
 
 
     // Game Manager Script for score
     GameManagerScript GM;
 
 
     public Collider2D[] checkForSurroundingDarts;
 
 
     private void Start()
     {
         boardTransform = GameObject.FindGameObjectWithTag("Board").transform;
         thisDartTransform = gameObject.transform;
         thisLayer = gameObject.layer;
 
         GM = GameObject.FindObjectOfType<GameManagerScript>();
     }
 
 
     private void Update()
     {
         if (gameObject.tag == "Dart")
         {
 
             // Distance between the board and this dart
             float distance = Vector3.Distance(boardTransform.position, thisDartTransform.position);
             if (distance > boardContact)
             {
                 // Move Dart
                 transform.Translate(new Vector3(0f, Velocity * Time.deltaTime, 0f));
             }
             else
             {
                 gameObject.tag = "Pinned";
                 Invoke("AttachDartToBoard", 0);
             }
         }
     }
 
     private void AttachDartToBoard()
     {
         this.gameObject.transform.SetParent(boardTransform);
         GM.scoreValue++;
 
         checkForSurroundingDarts = Physics2D.OverlapCircleAll(transform.position, checkRadius);
         for (var i = 0; i < checkForSurroundingDarts.Length; i++)
         {
             if (checkForSurroundingDarts[i].gameObject.tag == "Pinned" && checkForSurroundingDarts[i].gameObject.layer == this.gameObject.layer)
             {
                 if (checkForSurroundingDarts[i].gameObject.GetComponent<DartOptimized>().Links.Count <= 1)
                 {
                     Links.Add(checkForSurroundingDarts[i].gameObject);
                 }
                 else if (checkForSurroundingDarts[i].gameObject.GetComponent<DartOptimized>().Links.Count > 1)
                 {
                     Links.Add(checkForSurroundingDarts[i].gameObject);
 
                     
                     //Links.AddRange(checkForSurroundingDarts[i].gameObject.GetComponent<DartOptimized>().Links);
                 }
             }
         }
     }][1]


[1]: /storage/temp/118843-links.png

links.png (37.8 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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by bakir-omarov · Jun 13, 2018 at 02:15 PM

List have a function Contains, it will check if Object is in list or not.


 public class MyCode: MonoBehaviour
 {
 
     private List<GameObject> m_myList;
     private GameObject m_myGameObject;
 
 
     void Check()
     {
         if (ListContains(m_myList, m_myGameObject))
             Debug.Log("It already has this Gameobjet");
         else
             Debug.Log("We don't have this GameObject in this list");
     }
 
     private bool ListContains(List<GameObject> _list,GameObject _gameObj)
     {
         if (_list.Contains(_gameObj))     
             return true;
         else
             return false;
     }
 }


UPDATED:
So if you want to compare listOne and listTwo, and add all elements that is not in List Two, just use:


     void Check(List<GameObject> _listOne, List<GameObject> _listTwo)
     {
         foreach (GameObject _GO in _listOne)
         {
             if (!_listTwo.Contains(_GO))
                 _listTwo.Add(_GO);
         }
     }

Comment
Add comment · Show 4 · Share
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 Dorinbuzilov · Jun 13, 2018 at 02:43 PM 1
Share

Thanks for the response, I was told that using foreach isn't so good because it will create garbage that will slow down the application later?

avatar image WinterboltGames Dorinbuzilov · Jun 13, 2018 at 02:59 PM 0
Share

it's very un-noticeable if you didn't do it too much and never ever make a for-each loop in Update, FixedUpdate or LateUpdate

avatar image Dorinbuzilov WinterboltGames · Jun 13, 2018 at 04:27 PM 0
Share

Is it still ok to do this if the second list im trying to compare to is part of another instance.

Show more comments

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

89 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

Related Questions

How to search a certain variable in a list of a created class (C#) 1 Answer

bool to true from a list comparison. 1 Answer

A node in a childnode? 1 Answer

String list contains method returns false from game object name 0 Answers

How to return index of List element? 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