code works, bug was elsewhere in the code, thanks to those who helped, was useful to debug
So I have a number of objects, each with a collision trigger to detect when another object gets within a certain radius. It then broadcasts a message to add the object (technically a reference or pointer or whatever I suppose), to an ArrayList with the following code:
//collision trigger structured for modular use (irrelevant to question as far as I know)
function OnTriggerEnter (other: Collider) {
transform.root.BroadcastMessage("AddMatchNeighbor", other.gameObject.transform.root.gameObject, SendMessageOptions.DontRequireReceiver);
}
//script receiving broadcast
function AddMatchNeighbor (other: GameObject) {
if ( other in matchNeighbors == false) {
matchNeighbors.Add(other);
} else {
Debug.Log("Match duplicate");
}
}
Objects are removed from the array in a similar fashion OnTriggerExit(). When one of the objects is clicked, it then checks to see if there are 2 or more objects in the array (i.e. 3 or more objects are close to each other) and if so, score is awarded the player and the objects are destroyed (think Match 3 style game mechanic).
The problem is that the ArrayList (matchNeighbors) isn’t being reliable in it’s accounting and it will get longer overtime with duplicate objects (which my code seems to prevent most of the time, but apparently not all). Any idea what the problem might be?
You could compare the object’s ID. If the ID is the same, then it is the same object. You can get the object’s ID using [gameObject.GetInstanceID][1].
I wrote some code for you, heavily commented (although my js is not that great):
function AddMatchNeighbor (other: GameObject) {
//In the start of the function, create a boolean to determine if the object can be added, and set it to false.
var canAdd : boolean = false;
//Create a `for` loop to check if the object you're about to add has the same ID as any of the existing ones in the array.
for ( var i : int; i < matchNeighbors.length; i++ ) {
if ( matchNeighbors*.GetInstanceID() != other.GetInstanceID() ) {*
//If the object’s IDs do not match, set the variable to true for the time being. canAdd = true; } else { //Object with the same ID as the ‘other’ is found. //Set the variable to false, and stop the loop. canAdd=false; break; } } //If the loop was stopped or finished, check the final value of the variable, and add the object if the condition is true. if( canAdd == true ){ matchNeighbors.Add(other); } else { Debug.Log(“The object is already in the array, and won’t be added again!”); } } [1]: Unity - Scripting API: Object.GetInstanceID
Why are you using an ArrayList, use a List<T> its almost always the better choice.
If they need to be unique, find a unique identifier and then use as the key in a Dictionary<TKey, TValue> it will be much faster to reference them and check if the id is already in the dict
As mentioned GameObjects have ids, you could use that then use ContainsKey to do a lookup and then the indexer to get or set the value.
Why does this works? Because Dictionaries can only have unique keys.
Why is this better? It means checking that its unique is less time complex O(1) vs O(n)