adding and removing objects from array

Hi all,

I’m trying to make a selection manager, and as part of that, check if an object is already part of an array and if so remove it, and conversely, check if an object is not yet in the array, and then add it. However, the code I have won’t add anything to the array, regardless of how many times I click or what I click on. The array remains empty. If I remove the if and else if chunks and move the Add outside of the for loop it works of course, but doesn’t give me the functionality I need.

var hitinfo : RaycastHit;
var shipID;
var selectionArray : Array;
 
function Start () {
        selectionArray = new Array();
}
 
function Update () {
        if (Input.GetMouseButtonDown(0)) {
                var cameraRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast (cameraRay, hitinfo)) {
                        shipID = hitinfo.collider.transform.parent.gameObject.GetInstanceID().ToString();
                        for (var i = 0; i < selectionArray.length; i++) {
                                if (selectionArray *== shipID) {*

selectionArray.RemoveAt(i);
}
else if(selectionArray != shipID) {
selectionArray.Add(shipID);
}
}
}
print(selectionArray);
}
}
Any help is much appreciated!

Well your array doesn’t have anything in it to start with so it cant iterate because there is nothing there.