- Home /
Bool condition is always resulting in false even when it seems impossible to be false even using debug lines to check
Basically i have an enum called 'type' in my Item class, which tells me if the item is consumable, or equipment, etc. In the database script I have made, it sets the type for each item (5 in total). So basically I have an inventory UI, with 5 items (inc. 2x consumables, 3x Equip)
After much debugging/checking things, I've found that the boolean prior to "sortedItems.Add(i);" is always FALSE. But it shouldnt be, BOTH VALUES ARE THE SAME as per the console log. I have input a print() line to check whats going on because my items are'nt sorting by type when I expect them to.
public void SortItemsByType(string type)
{
sortedItems.Clear();
foreach (Item i in allItems)
{
print("is the following true or false" + i.type.ToString() == type);
print("this is iToString= " + i.type.ToString());
print("and this is type: " + type);
if (i.type.ToString() == type)
sortedItems.Add(i);
}
}
and (please note the 3 print lines, here is their output in the console: (sorry but the dumb IDE won't let me copy-paste the console, here is image):
Im still very much a noob to this, so I am sure there is something I have missed. But for the life of me i cannot figure out why that bool is always resulting in FALSE?
The console even says 1. This formula is false 2. a=equip , b=equip?, it makes no sense to me
Any help is massively appreciated. Thanks
Answer by Kishotta · Jul 16, 2017 at 09:39 PM
In the condition:
if (i.type.ToString() == type) {
// ...
}
You are comparing a string and an enumerable type (which is technically an int). These will never be the same, because they're not even the same data type. ToString() is only used to convert the name of that enumeration option to a string for the purpose of printing/logging.
You should use:
if (i.type == type) {
// ...
}
hi thanks for the quick answer. Im afraid that isnt the case though. Id orginally not used the ToString() method. But if you look closely the second 'type' in the bool is the String from the method signature. And it is a string, not type. If I try the code you said I get error: "Assets/Inventory/Scripts/GameDB.cs(75,19): error CS0019: Operator ==' cannot be applied to operands of type
Item.Type' and `string'"
Thank you though, your intention to help is much appreciated
Ah, I missed it in the unformatted code.
It looks like string equality checking might be acting a little funky. Try:
if (i.type.ToString().CompareTo (type) == 0) {
// ...
}
this worked. I still dont have the functionality i wanted but at least the bool is reactinf how i expected it to now . thanks again pals
Answer by Bunny83 · Jul 16, 2017 at 10:19 PM
The enum to string conversion will always work properly. That means your string doesn't match properly.
Where does the string come from which you pass to "SortItemsByType"? Try this instead:
Debug.Log("type: >"+type+"< ("+type.Length+")");
Tell us what this will print for you. Maybe there's an additional space or newline character at the end?
To be 100% sure you can use this:
string tmp = "";
foreach(var c in type)
{
tmp += ((int)c).ToString("X4") + " ";
}
Debug.Log("type: >"+type+"< ("+type.Length+") \n" + tmp);
Try this and copy the output here in a comment. But you most likely will notice that there is something wrong with your string.
Answer by megabrobro · Jul 16, 2017 at 10:54 PM
Just to clarify if a futur reader has a similar issue. The strings looked identical even in the print log (i didnt bother to use debug log, as the issue was solved by changing it to this line....
if (string.Equals(i.type.ToString(), type))
sortedItems.Add(i);
This doesn't change anything. This will result in the same behaviour as using "==".
The reason why this prints false:
print("is the following true or false" + i.type.ToString() == type);
Is because "+" is evaluated before "==" due to the order of operators. That means you first combine the string "is the following true or false" with your enum string and then compare it to your string which of course will yield "false". Also have a close look at your debug output. It doesn't print "is the following true or false" but only "false". You would need to do:
print("is the following true or false: " + (i.type.ToString() == type));
Note the brackets.
Answer by Cornelis-de-Jager · Jul 17, 2017 at 04:07 AM
If you are looking into using Types, then there is a better method to use: Enums. Create a new file with the following code within it, as is.
using UnityEngine;
using System;
using System.Collections;
public enum MyTypes {
// Do this Alphabetically
TypeA,
TypeB,
TypeN
}
Safe as MyTypes.cs - To add the property to your Item class us:
public Types type;
void Start () {
type = Types.typeA;
}
Now you can just call it from the Sorting list:
public void SortItemsByType(Types type)
{
sortedItems.Clear();
foreach (Item i in allItems)
if (i.type == type)
sortedItems.Add(i);
Your answer
Follow this Question
Related Questions
prefabs inside content area not fitting entire width of content area? 0 Answers
get_version can only be called from the main thread. 0 Answers
How to make a Panel (Or Scrollbar) Appear on Button Click 0 Answers
Unity 5: UI button OnPointerDown not function as expected 1 Answer
Text UI broken after changing alpha color of single letters 1 Answer