- Home /
The question is answered, right answer was accepted
Count Variable Acting Strange
CODE:
public void InventoryWindow(int id){
int cnt = 0;
string itemToolTip;
for(int y = 0; y < _inventoryRows; y++){
for(int x = 0; x < _inventoryCols; x++){
if(cnt < inv.Stacks.Count && inv.Stacks[cnt] != null){
if(!editing){
itemToolTip = "<b>" + inv.Stacks[cnt].Item.Name + "</b> \n " + inv.Stacks[cnt].Item.Description + " \n <size=10>" + inv.Stacks[cnt].Item.Type + "</size>";
} else{itemToolTip = "";}
if(GUI.Button(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), new GUIContent(inv.Stacks[cnt].Item.Icon, itemToolTip), "box")){
editing = true;
}
if(editing){
GUI.BeginGroup(new Rect(32.5f, 280, 200, 90), comboBacker);
for(int i = 0 ; i < items.Length; i++){
int index = cnt;
print(cnt + " = Index!");//Normal here********
if(GUI.Button(new Rect(0, (30 * i), 200, 30), items[i], "button")){
print(cnt + " = COUNT!!!!");//Always Zero here********
selectedItem = items[i];
editing = false;
switch(selectedItem){
case "Use":
//print ("Use Item");//Use Item
UseItem(inv.Stacks[index].Item);
break;
case "Drop":
//print ("Drop Item");//Drop Item
break;
case "Cancel":
//Do nothing, just close options
break;
default:
//Do nothing, just close options
break;
}
}
}
GUI.EndGroup();
}
GUI.Label(new Rect(6 + (x * buttonWidth), 20 + (y * buttonHeight), 50, 50), inv.Stacks[cnt].Size.ToString(), "label");
//print (inv.Stacks[cnt].Item.Name);
} else if(inv.Stacks[cnt] == null){
GUI.Button(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), (x + y * _inventoryCols).ToString(), "box");
}
//print(cnt);
cnt++;
}
}
Tooltip.Instance.TooltipGUI();
GUI.DragWindow();
}
For some reason when i try to reference the 'cnt' variable at the switch statement it always equals ZERO?
About 4/5 lines from bottom i have a print which is working fine and the 'cnt' variales is working, but why not for the switch statement?
EDIT:
The 'cnt' variable stops working after line 18, but works fine before it. *Added Whole Function
you don't reference the cnt variable at the switch statement. Also, you don't show where you define the variable. There are no Debug.Logs etc. Have you done some basic debugging like Debug-Log it, to seee where it changes? if so, you should have a line on which it still works and a next line where the value is wrong. Please find out
on the same road as stuart6854, I would check the cnt in the first if statement. I'm afraid that there is not enough code because we can't see where cnt comes from, it's maybe null or zero from the init.
I suggest you put some breakpoints and check the value and track along the code.
If that didn't help, please provide more code but that might not help that much because it seems coupled with some other parts.
Cheers.
Well, that is strange. Have you tried attaching to unity and debugging it step by step?
Answer by stuart6854 · Jan 06, 2014 at 04:43 PM
OK, i have worked out the silly problem :P
It was because i was using 'cnt' to reference the item which was ever changing while the user selected a drop down box option(Dunno why it was always 0 though?), so when the userclicked on the inventoryitem i needed to store the current index to stop it changing.
Thanks anyway :P
Follow this Question
Related Questions
Function with a variable text in the middle of it 1 Answer
Finding Closest Enemy... 2 Answers
Way to print and email from Unity application? 1 Answer
Unity's Equivalent of Console.Write(); 1 Answer
print the mass values of rigidbody attached with the gameobjects assigned to the array variable 1 Answer