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 /
avatar image
1
Question by AwesomeFaceHD · Mar 12, 2014 at 04:34 AM · c#inventoryinheritanceclassesenum

Problem accessing enum from other class

I'm trying to check the enum of an item from a list of a class called Item. I get an error that says it can't access the static member with an instance reference.

Here are the scripts:

Item class:

 [System.Serializable]
 public class Item {
     public string name;
     public string desc;
     public Rarity rarity;
     public ItemType type;
     public Texture2D icon;
     public bool equipped;
 
     public void ApplyState() {
         if (equipped) {
             Debug.Log("Add Stats");
         } else {
             Debug.Log("Remove Stats");
         }
     }
 }
 
 //Item Rarity
 public enum Rarity {
     common, //White
     uncommon, //Blue
     rare, //Yellow
     legendary //Orange
 }
 
 //Item Type
 public enum ItemType {
     tool,
     weapon,
     food,
     misc,
     head,
     body,
     legs,
     feet,
     hand,
     neck
 }

part of Inventory Class:

 public void InvenTool() {
         GUI.Box (new Rect(0, 0, 200, Screen.height), "Tools");
         for(int x = 0;x < items.Count;x++) {
             if(items[x].type == items[x].type.tool) {
                 if(GUILayout.Button("" + items[x].name)) {
                     Debug.Log(items[x].desc);
                 }
             }
         }
         if(GUI.Button (new Rect(10, 190, 180, 30), "Back")) {
             NavigateTo("Sel");
         }
     }

I don't know what the problem is. I read that I'm supposed to be able to access enums from all classes, but it doesn't work. I would appreciate help with this.

Comment
Add comment · Show 6
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 Nanobrain · Mar 12, 2014 at 05:04 AM 0
Share

The code above shows your enums to be outside of your Item class. Place them inside.

avatar image Deadcow_ · Mar 12, 2014 at 05:22 AM 2
Share

ins$$anonymous$$d of

 items[x].type == items[x].type.tool

try

 items[x].type == ItemType.tool
avatar image AwesomeFaceHD · Mar 12, 2014 at 05:35 AM 0
Share

@Deadcow_ Thank you so much, it worked!

avatar image highpockets · Mar 12, 2014 at 05:40 AM 1
Share

$$anonymous$$ake this question solved so people, like myself, don't think it is still unanswered.

avatar image AwesomeFaceHD · Mar 12, 2014 at 06:43 AM 0
Share

How do I do that without an answer?

Show more comments

1 Reply

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

Answer by Deadcow_ · Mar 12, 2014 at 06:52 AM

instead of

 items[x].type == items[x].type.tool

try

 items[x].type == ItemType.tool
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 AwesomeFaceHD · Mar 12, 2014 at 07:08 AM 1
Share

Thank You :)

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

23 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Add random amount of random items from one list to another? 2 Answers

How to ignore base class? 1 Answer

Problem using variable of type CustomStat to indicate which player statistic to change 1 Answer

FPS weapons class structure 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