- Home /
Unity completely defys logic in this if statement.
Hello im having trouble with this piece of code im trying to formulate for an inventory system i got. I am making a system where if you have a bag on and you are carrying more items than your default inventory can hold, the bag cannot be unequiped.
if(i.itemType == "Backpack") // if the item is a backpack
{
Debug.Log("is backpack");
if(playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots) == true);
//Basically if the current inventory array contains less items than the default inventory
{
Debug.Log("Current Inventory Content" + playersinv.Contents.Length + " Bag total slots " + playersinv.MaxContent + " Extra slots " + i.GetComponent(i.ItemName + "_script").BagExtraSlots);
canUnequip = false; // stop me from unequiping.
}
}
Basically my equation for detering whether or not the bag has enough space returns false to the following logical statement 0 < 2. Ive even substitued in 5 < 2 whic it totaly isnt and it still thinks it = true!?
What the hell am i doing wrong? All i want it to do is
if(CurrentArrayofObjects.Length > CurrentBagSize - AddedBagSlots)
{
CanUnEquip = false;
}
even if i add If(blablabla == true)
it still spazzes out....
Any help???
Semicolon after second if statement. Also never write == true in an if statement. That is implicit. Not sure if this solves the problem.
Answer by AndyMartin458 · Jan 22, 2014 at 02:28 AM
The semicolon is separating your if statement from the block {}. Regardless of if the statment evaluates to true or false, you will enter the block.
You also should use parentheses to explicitly show what it is you are comparing. if(playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots) == true); // Wrong, don't use a semicolon
if( (playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots)) == true)
{
}