- Home /
Add object into an array without continuously adding it or looping.
I am trying to add an item in an array only once a concept witch seems to be to complicated for me.Every time i add an item to an array it continuously loops , adding the item.
I want to know how to add the item only once but without using the if function :
if(array.length > 0) array.Add(item)
The array that i am using is dynamic and if i use a true/false statement it always adds at least 16 copies of the same item. An example code would be nice.
Comment
It is unclear what you want to have here. Are you checking in Update? When do you want to add something? Please explain a bit more and put up the code you are currently trying.
~Justin
Best Answer
Answer by programmrzinc · Mar 08, 2013 at 11:38 PM
var myArraylist : ArrayList;
var item : int;
var case : string;
function Start (){
myArrayList = new ArrayList();
ChangeItem(case);
}
function ChangeItem(var caseValue : string){
if(caseValue == "a"){
myArrayList.add(1);
}
if(caseValue == "b"){
myArrayList.add(2);
}
if(caseValue == "c"){
myArrayList.add(3);
}
case = "";
}
//You can call the ChangeItem at any time, but because case is reset to an empty value, It wont add anything to the array, making it only add once.