• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by megabrobro · Jul 16, 2017 at 09:32 PM · c#unity 5uibooleandatabase

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): console output

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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) {
     // ...
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image megabrobro · Jul 16, 2017 at 09:49 PM 0
Share

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

avatar image Kishotta megabrobro · Jul 16, 2017 at 10:17 PM 1
Share

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) {
     // ...
 }
avatar image megabrobro Kishotta · Jul 16, 2017 at 10:52 PM 0
Share

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

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 16, 2017 at 11:51 PM 1
Share

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.

avatar image
0

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);
  

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

390 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with implementing my own slider script. 1 Answer

Button only works once 1 Answer

How do I save and load the value of a text? 1 Answer

[C#] Unity asks for a } for a reason I don't know ( '}' expected ) 1 Answer

How to make buttons have sound when it is highlighted and clicked? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges