How to add to a list excluding certain items

Hi, I’m trying to make a system that will give me 5 items from a list but exclude items with the same id so that I don’t get any repeats. The system gives me 5 items but I keep getting repeats and I don’t know what I’m doing wrong.

Here is the code from my system:


static function SetAvailableMissions(){

		usedIDs.Clear();
		usedTypes.Clear();
		availableMissions.Clear();

		for (var tempMission:Mission in activeMissions){
			if (tempMission != null){
				usedIDs.Add(tempMission.ID);
				usedTypes.Add(tempMission.type);
			}

		}

		for (var tempMission:Mission in missions){			
			
			
			if (usedIDs.Count){
				for (var i : int = 0; i < usedIDs.Count; i++){

					if(tempMission.ID == usedIDs*){*
  •   					continue;		*
    
  •   				}*
    

if(tempMission.type == usedTypes*){*
* continue;*
* }*
* availableMissions.Add(tempMission);*
* }*
* } else {*
* availableMissions.Add(tempMission);*
* }*
* }*
* } *

* static function ReplaceMission(position:int) {*
* SetAvailableMissions();*
* var tempRandom = Random.Range(0,availableMissions.Count);*
* activeMissions[position] = availableMissions[tempRandom];*
* activeMissions[position].position = position;*
* }*

* static function CheckActiveMissions() {*

* for (var i : int = 0; i < activeMissions.length; i++){*
_ if (!activeMissions*){
ReplaceMission(i);
}
}
}
----------
this is the Mission class used:
----------
class Mission {
var ID :int;
var stringID :String;
var type :int;
var targetValue :int;
var position :int = 0; //Position in the active mission list*_

* function Mission (thisID:int, thisStringID:String, thisType:int, thisTargetValue:int) {*

* ID = thisID;*
* stringID = thisStringID;*
* type = thisType;*
* targetValue = thisTargetValue; *

* } *
}
This is how the list of missions is inserted:
* MissionManager.AddMission(0,“text”,1,10);*
* MissionManager.AddMission(1,“text”,1,10);*
* MissionManager.AddMission(2,“text”,2,30);*
* MissionManager.AddMission(3,“text”,2,40);*
* MissionManager.AddMission(4,“text”,3,140);*
* MissionManager.AddMission(5,“text”,3,100);*
* MissionManager.AddMission(6,“text”,4,300);*
* MissionManager.AddMission(7,“text”,4,400);*
* MissionManager.AddMission(8,“text”,5,1400);*
* MissionManager.AddMission(9,“text”,5,1400);*
* MissionManager.Start();*
after executing MissionManager.Start();, activeMissions sometimes has Missions with the same ID or type.
Any ideas how to do this correctly?

If you’re trying to avoid repeats, I’d recommend not using lists. There are data structures, such as Dictionaries and Sets, that by definition cannot contain repeats. While Sets are also unordered, Dictionaries have the concept of a “key”, which you can use to index into them. I suggest using one of those.

Having said that, I don’t see, at a glance, anything that could add multiple missions, but this may be because none of the code you’ve shown actually does anything to the list of active missions.

Also, you should never call Start manually - Start is called by Unity when the object is created, calling it twice can cause all sorts of errors. If you need something initialised before Start occurs, try putting it in the Awake method (which is called before Start, but doesn’t guarantee all scene objects are available yet).