- Home /
The question is answered, right answer was accepted
Glitch in Monodevelop for loops!!!
Predict the output before you read the bottom!
private InventorySlot[] FillInventoryWithJunk (int Cols, int Rows, int storageType, InventoryItem junk, int[] skipSlots)
{
InventorySlot[] itemsStorage = new InventorySlot[Rows * Cols];
for(int i = 0; i < itemsStorage.Length; i++)
{
itemsStorage[i] = new InventorySlot(junk);
itemsStorage[i].Item.ID = i;
}
for(int i = 0; i < Rows * Cols; i++)
{
Debug.Log(itemsStorage[i].Item.ID);
}
return itemsStorage;
}
Should the output not be 0, 1, 2, 3, 4, 5? What is actually output is "5, 5, 5, 5, 5". If the ID's were not changed it would output all 1's as thats what the "Junk" item ID is. If the debug.log is placed in the first loop it outputs correctly. If the ID assignment line is place in a separate loop, it still outputs not as expected in the debug loop. Ive been stuck on this for days. Any help would be amazing. Thanks in advance.
Answer by njpatter · Jan 22, 2014 at 10:43 PM
You are setting the Item.ID on the same 'junk' object each time through. That's why it says 5,5,5,5,5 instead of 0,1,2,3,4.
Try copying/cloning/instantiating the prefab/dummy 'junk' object each time you want to add it to the array instead of using the same reference for all array elements.
Alight thank you, this is day 3 of this glitch so your response is greatly appreciated. I'm still trying to process this though. By how do I copy/clone it each time I add it to the array? I thought the new keyword did that. How would you go about doing this?
Could you post the code from the InventorySlot constructor?
public InventoryItem Item;
public int Type; //0 is normal, 1 is hand
//Constructors
public InventorySlot (InventoryItem _Item)
{
this.Item = _Item;
this.Type = 0;
}
public InventorySlot (InventoryItem _Item, int _Type)
{
this.Item = _Item;
this.Type = _Type;
}
I think you want to see the inventory item. Each slot holds an item. Just let me know and ill post that too!
Ok, look at lines 7 and 12. When you pass the 'junk' object into the constructors the 'this.Item' is being set to a reference of the original 'junk' object.
This means that when you go to set the Item's ID, (0,1,2,3,4,5), the referenced object (which you are setting to be the same exact one by not copying/cloning it) has its ID changed. That is why they are all reporting '5' because you are asking the same object what its ID is 6 times (5,5,5,5,5,5).
To correct this you could copy the 'junk' item into a new instance of an InventoryItem in the InventorySlot constructor OR when you pass it to the constructor...
itemsStorage[i] = new InventorySlot(new InventoryItem(junk));
That functionality may or may not be supported by the InventoryItem class but it should be an easy addition. Let me know if you've got any other questions
Follow this Question
Related Questions
Autocomplete a for-loop in MonoDevelop? 1 Answer
Create a Pause In A For Loop 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cant get Monodevolop to work 0 Answers