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
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) {
// ...
}
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+")
" + 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.
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);
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);