- Home /
Could you help me with Array.RemoveAt()?
I have an array I am finding the index to and then removing the item at the index. I have a custom function that finds the index of an item in the array. This is working fine (used console prints to test it). The index is a valid index number AND is the object I want, however when I go to Array.RemoveAt(index) I get an OutOfRange error.
The function that is causing errors is....
function AddBrokenConnection(broke : GameObject, temp : GameObject){
var brokeIndex : int;
brokeIndex = FindIndex(broke,realConnections);
if(brokeIndex != -1){
realConnections.RemoveAt(brokeIndex);
}
brokenConnections.Add(broke);
tempConnections.Add(temp);
}
FindIndex is the function that finds the index. It returns -1 if the function can't find it in the array. The array length returns as 2. The index number I am removing is 1. So I fail to see why it is throwing errors.
I see nothing obviously wrong with the code. Sometimes when I have errors like this I find I've attached the script to more than one object, and it is the unknown component attached that is throwing the error.
You will save yourself a huge amount of grief if you never use Array, and always use a generic List ins$$anonymous$$d. You wouldn't need to bother with any of this, but could just remove the GameObject directly. e.g., assu$$anonymous$$g you have a List.< GameObject >
called "goList", then just do goList.Remove(myGameObject)
. Plus you have built-in functions for getting item indices (namely IndexOf) if you ever do need the index.
I've not used Generic Lists, how would I go about creating one.
Your answer
Follow this Question
Related Questions
loading a folder of textures to an array 0 Answers
Array of transform index out of range error 1 Answer
playing a different animation in an array 1 Answer
how to index through game objects? 1 Answer