• 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
Question by Mahunreah · Apr 27, 2018 at 10:10 PM · enumforeachequalenumerate

I want to compare the value of two enums with one another but struggle in how to accomplish it. Might need a bit help in understanding why.

Hi =)

I have a shop, which generates its buyable items based on which kind of category of wares it belongs to. For examle: One shop only sells every item from the "craftmaterial" category, while another only sells "weapons". Up until now, I used strings for this and it worked. But I would rather use enums, since I'm sure that someday, I will misspell "craftingmaterial" and wonder for hours why it doesn't show up in my shop.

So, on the side of my items, I wrote this (it's an excerpt):

 using UnityEngine;
 
 [CreateAssetMenu]
 public class Item : ScriptableObject
 {
     public enum Warengruppe { Craftingmaterial, Weapons }; 
     public Warengruppe warengruppe;
 }

The shop-script looks like this (again, an excerpt)

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ShopSlotGrid : MonoBehaviour
 {    
     public enum Warengruppe { Craftingmaterial, Weapons };
     public Warengruppe warengruppe; 

     void Start () {
         itemsInGame = GameObject.FindGameObjectWithTag ("ItemsInGame").GetComponent<ItemsInGame> ();
 
         foreach (Item i in itemsInGame.items)
         {
             if (i.warengruppe == warengruppe) // TODO Hier zu enum ändern (change this to enum)?
             {
                 shopSlots.Add(Instantiate(shopslotPrefab, shopslotGrid));
                 dieseShopAuslage.Add(i);
             }
         }
 }

Right now, I get this error:

Operator ==' cannot be applied to operands of type Item.Warengruppe' and `ShopSlotGrid.Warengruppe'

First, I'm rather sure that using two seemingly equal enums like this is probably a bad idea? Second: Why can I not use the "=="-operator in this case? I've used this operator in other places of my game successfully and the only difference I can see is that this is the only place where I'm using a "for each" loop :/

If it helps: I use this script on every shop in my game, so I would like to choose which item category belongs to each of them by hand in the inspector and not inside the code.

Would love to hear your input :)

Comment
Bunny83

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Apr 27, 2018 at 10:25 PM

You defined two seperate enum types. One that is a nested type under your Item class and one that is a nested type under your "ShopSlotGrid". Those are two completely seperate types. Just because you named them the same doesn't make it the same type. You may want to define that enum type outside of your class and let both classes use the same enum type. The cleanest solution would be to create a "Warengruppe.cs" file and just put your enum definition in there. Of course remove the nested types and just use that one.


ps:
As a german myself i would strongly recommend to rename that enum to something like ItemGroup ^^ Especially since the members of that enum are english.

Comment
Buckslice
Mahunreah

People who like this

2 Show 2 · 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 · Apr 27, 2018 at 10:31 PM 0
Share

If you don't want to create a seperate file for the enum you can declare it in one of the two class files. You can even keep it a nested type if you like. Though nesting should be used carefully. When you create a nested type that type should be strongly bound to the outerclass. In this case i would recommend to declare it inside the Item class and name it just "Group". You can use the enum type every where by using Item.Group

 public class Item : ScriptableObject
 {
     public enum Group { Craftingmaterial, Weapons }; 
     // [ ... ]
 
 public class ShopSlotGrid : MonoBehaviour
 {    
     public Item.Group group; 
     // [ ... ]

avatar image Mahunreah · Apr 27, 2018 at 10:40 PM 0
Share

Aaaaaah, thank you! I always wondered why I should declare enums outside of a class (tutorials like to not mention that part of enums, sadly). I've done what you said and now, it works! And I think, I understand enums a bit better than before, thank you!

Oh, and... yeah, my code is a bit of a bi-lingual mess ;) I rework the parts that work into something a bit more consistent on Sundays and I think I will put "rename stuff" on the list, too ;)

avatar image

Answer by Buckslice · Apr 27, 2018 at 10:26 PM

You should just have one version of the enum, you cant compare different enums together really, and there is no point in having two enums here because they are exactly the same. Just use Item.Warengruppe since the categories seem to designate what type of items they are. In line 9 of shop code just say this instead

 public Item.Warengruppe warengruppe;

Hope this helps

Comment
Mahunreah

People who like this

1 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 Mahunreah · Apr 27, 2018 at 10:42 PM 0
Share

Thank you for your input =)! I wasn't really aware that I had two different enums since they sounded similar. Oh well, beginner mistakes ;)

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

84 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

Related Questions

Enums updating, but then reverting 1 Answer

!= operator syntax for switch statements. (does not equal operator) 2 Answers

Using Enum With Different Scrits 2 Answers

Hexes not equal to themselves 1 Answer

Enum with multiple fields 1 Answer


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