Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by sk8terboy4 · Jan 21, 2015 at 03:17 AM · errorinventoryenumstruct

Error with Structs and Enums

Im making a inventory system for my game and i am trying to check if an item type is equal to the same type. I have a struct with enums:

 [System.Serializable]
 public class Item {
 #region vars
     public string itemName, itemDesc;
     public int itemID;
     public Sprite itemIcon;
     public GameObject itemModel;
     public int value, amount;
     public ItemTypes itemType;
 #endregion
 #region ItemTypes
     public struct ItemTypes
         {
         public enum Consumable { Health, SpeedPotion, Food, JumpPotion, SkillPotion,}
         public enum Weapon { Automatic, Sniper, Pistol, Shotgun, }
         public enum Armor { Head, Chest, Pants, Shoes, }
         public enum Craftable { Stick, Cloths, Rope, Rock}
         }
 #endregion

then I check the item type:

 if (inventory.items [slotNumber].itemType == Item.ItemTypes.Consumable) //Error here
             {
                 
                 inventory.items[slotNumber].amount--;
                 
                 if(inventory.items[slotNumber].amount == 0)
                 {
                     inventory.items[slotNumber] = new Item();
                     itemAmount.enabled = false;
                     inventory.ItemOptions.GetComponent<ItemUse> ().usedItem = false;
                 }
                 inventory.ItemOptions.GetComponent<ItemUse> ().usedItem = false;
             }
             else
             {
                 Debug.Log("Cannot use Item");
             }

I want to take the easy way but it's not letting me. I dont want to do "Item.ItemTypes.Consumable.Food", "Item.ItemTypes.Consumable.Health", etc. just to check if an item is a consumable. Thanks for the help.

Error: Assets/InventoryScripts/HotBarItem.cs(28,60): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Landern · Jan 21, 2015 at 03:22 AM

That isn't how enums work. You defined four enum types in a struct, you're checking the enum type Consumable which is an enumeration of integers(the default type for an enum numerical representation) represented in code by Names such as Health, SpeedPotion, Food, JumpPotion, SkillPotion. In order to check itemType, it would have to be comparable to a type ItemTypes that derives from ValueType in c#. But structs can't be inherited whatsoever, but do inherit from type ValueType. There isn't a reason to have a struct with enums in it, you're not defining anything special at all, enums derive from Enum and are beyond optimized. You have complicated something that shouldn't be complicated.

Comment
Add comment · 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 · Jan 21, 2015 at 03:35 AM 1
Share

Some additional points:

  • structs aren's serialized / serializable by Unity.

  • Even if you turn your ItemTypes struct into a class to make it serializable, the ItenTypes class / struct is actually empty. It doesn't have any information stored in it. All you have there are enum declarations which are types and not fields. So an instance of ItenTypes contains 0 information.

  • You might want to create derived classes from your Item class for each category. Or, if an item can actually be in two categories, implement a strategy pattern (just like Unity's component system) to describe what behaviour an item should have

  • Another way is to use only one enum called ItemType and define all possible types in there. You could arrage them like a bitmask.

Example:

 public enum ItemType
 {
     Consumable    = 0x00010000,
     Health         = 0x00010001,
     SpeedPotion    = 0x00010002,
     Food           = 0x00010003,
     JumpPotion     = 0x00010004,
     SkillPotion    = 0x00010005,
     
     Weapon         = 0x00020000,
     Automatic      = 0x00020001,
     Sniper         = 0x00020002,
     Pistol         = 0x00020003,
     Shotgun        = 0x00020004,
     
     Armor          = 0x00030000,
     Head           = 0x00030001,
     Chest          = 0x00030002,
     Pants          = 0x00030003,
     Shoes          = 0x00030004,
     
     Craftable      = 0x00040000,
     Stick          = 0x00040001,
     Cloths         = 0x00040002,
     Rope           = 0x00040003,
     Rock           = 0x00040004
 }

Here you can use a bit test to see which category an item is in:

 ItemType type = ItemType.Health;
 
 if(type & ItemType.Consumable != 0)
     // item is a Consumable
 if(type & ItemType.Weapon != 0)
     // item is a Weapon

avatar image sk8terboy4 · Jan 21, 2015 at 10:25 PM 0
Share

Bunny, the last part of your answer:

 ItemType type = ItemType.Health;
  
  if(type & ItemType.Consumable != 0)
      // item is a Consumable
  if(type & ItemType.Weapon != 0)
      // item is a Weapon

Edit: Never$$anonymous$$d But if i try to do: if(type & ItemType.Health != 0) It will allow All Consumables

avatar image
0

Answer by sk8terboy4 · Jan 21, 2015 at 04:19 AM

Thanks Landern, I knew I was doing something wrong with the enums and stuff just didn't know exactly. Thanks Bunny, I'm going to take a look at Strategy patterns and bits.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enum instead of numbers in arrays 1 Answer

enum comparing 1 Answer

Returning item names in an inventory array 3 Answers

Cannot Implicitly convert to bool problem? 1 Answer

Enum Type Inventory? 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