- Home /
Question by
BrimTheOne · May 16, 2014 at 12:46 AM ·
c#arraysinventoryfor-loop
[C#]For-loop in For-loop crashes Unity.
static public void addItemToINV(BASEITEM item){
BASEITEM tempitem = ItemRecognizer(item);
bool isAlready = false;
for(int i = 0;i < itemsSLOT.Length;i++){
isAlready=false;
if(itemsSLOT[i] != null){
if (itemsSLOT[i]._ID == tempitem._ID){
isAlready=true;
}
}
if(isAlready == true){
itemsSLOT[i]._QUANTITY += tempitem._QUANTITY;
break;
}
else if(isAlready == false){
if(i == itemsSLOT.Length - 1){
for(int j = 0;j < itemsSLOT.Length;i++){
if(itemsSLOT[j] == null){
itemsSLOT[j] = tempitem;//
break;
}
}
break;
}
}
}
}
When i run this method first time, it does at is it supposed to, without any problems.
However, the second time, it makes my Unity3D client crash.
I could not find another way of making this, that would not cause "item" to be added to the array, without checking through the whole array(itemsSlot) first. if anybody knows a way to do this w/o 2 For-loops, it would be greatly appreciated. or, know how to fix this.
Best regards.
Comment
Best Answer
Answer by rakkarage · May 16, 2014 at 12:48 AM
for(int j = 0;j < itemsSLOT.Length;i++){
should that be j++?
Your answer
![](https://koobas.hobune.stream/wayback/20220613145131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array index is out of range? 1 Answer
Null reference when editing an array in a for loop 1 Answer
Array Overflow Problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers