- Home /
Remove Object from a list if it is already in another list?
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";
}
Your answer
Follow this Question
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