- Home /
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
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);
}
}
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?
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
Is it still ok to do this if the second list im trying to compare to is part of another instance.