public void ReturnItemToInventory(GameObject objToReturn) { //int i = 0; Wire objsWire = null; // Destroy wires as well, if they exist if (objToReturn.transform.Find("Electrical") != null) { ConnectionPoint conPoint = objToReturn.transform.Find("Electrical").Find("ConnectionPoint").GetComponent <ConnectionPoint>(); if (conPoint.connectedWire != null) { objsWire = conPoint.connectedWire; } } Destroy(objToReturn); if (objsWire != null) { // This will check if both connection points are valid, they won't be, therefore // the wire will be deleted objsWire.DeleteNextFrame(); } // Look through spawnedObjects for objToReturn, and then break; foreach (SpawnedObject spawnedObject in spawnedObjects) { //i++; //Debug.Log(Time.frameCount + " | " + i.ToString() + " Looping through another spawned object..."); if (spawnedObject.ActualObject == objToReturn) { //Debug.Log(Time.frameCount + " | " + "Found the object to return on loop " + i.ToString()); foreach (InvItem invItem in itemSlots) { // Find the item slot of objToReturn and +1 to it if (invItem.uniqueName == spawnedObject.OriginInvSlot) { invItem.startingAmount++; invItem.UpdateAmountCounter(); break; } } // Remove objToReturns relevant SpawnedObject as otherwise errors will occur later spawnedObjects.Remove(spawnedObject); break; } } }