- Home /
Adding an object to an array only if it is not in the array
I am trying to add and object to an array. I am trying to check if the object is already in the array, if it isnt, then add it. If the object is in the array then I dont want to add it. The issue that I am running into, is that my script continuously adds the object to the array. Here is what I have so far :
for ( var i : int = 0; i < activearray.Length; i++ )
{
var selectedobject : GameObject = activearray[i];
if ( selectedobject == hit.collider.gameObject)
{
Debug.Log("tile is already grouped");
}
else
{
activearray += [hit.collider.gameObject];
}
}
Answer by dannyskim · Oct 27, 2012 at 11:16 PM
Unityscript Javascript arrays are slow, as far as I have read.
It's better to just simply use a list for this functionality, because it already has a method to check if an object exists in the list.
if( yourList.Contains( hit.collider.gameObject ) )
Debug.Log("Tile is already grouped");
else
yourList.Add( hit.collider.gameObject );
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
remember to add System.Collections.Generic to your import / using statements.
The code isn't using a Javascript array, it's a GameObject[] array.
Answer by Eric5h5 · Oct 27, 2012 at 11:21 PM
Imagine that you have an array of 10 entries, and the object in question is one entry in the array. Your code will loop through the array and add hit.collider.gameObject for every entry that's not the same game object, so it will be added 9 times.
You should use List instead of an array if you're adding items to a collection. In that case you can just use List.Contains to see if the object is in the List or not.
It seems to still add to the list continuously. What am I missing here?
function CastRay() { var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; var groupedTiles = new List.();
if(Physics.Raycast(ray, hit))
{
if(!activeobject)
{
activeobject = hit.collider.gameObject;
groupedTiles.Add(hit.collider.gameObject);
}
else if (hit.collider.gameObject.layer == activeobject.layer && !groupedTiles.Contains(hit.collider.gameObject))
{
groupedTiles.Add( hit.collider.gameObject );
numTiles +=1;
Debug.Log("tile added to group");
}
}
}
Actually, moving the declaration for the list outside of the function seems to have cleared that up. Thank Eric, you are extremely helpful. I guess my other question is how do I iterate through a list if it doesn't have length?
...List.Count, and you would simply access the object the same way as a built-in array, simply use the index number.
Ah, Count, thank you! It still seems a little strange to me .. a function like this
function CheckGroup() { for (var i=0; i < groupedTiles.Count; i++) { score+=1; groupedTiles.RemoveAt(i); } }
isnt actually clearing through the whole list. It works fine for one object, but if you do four or more, it clears only 2.
Because you're increasing i by 1 every time through the loop after removing the element at i. You can just use List.Clear to clear all items.
Your answer
Follow this Question
Related Questions
[Solved]Using multiple arrays in one statement? 1 Answer
How to correctly shorten this script using arrays and iterations 2 Answers
how do i make a loop for a boolean? 2 Answers
I dont know why im getting a null reference exception 1 Answer
How can I move back to Element 0 in an array to reset a loop? 1 Answer