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
0
Question by William_Weaver · May 12, 2014 at 11:05 AM · javascriptclassenumtypeitem

Need assistance with SubType in custom class (UnityScript or C#)

I have set up a simple item class that holds all the basic information of every item in my game(Name, Prefab path, Icon, Type, etc.) and I'm trying to work in subtypes. Currently the Type is a short enum {weapon, armor, material, item, misc}.

What I want to have is something in the class script that says "if Type.weapon, SubType enum {oneHandMelee, twoHandMelee, shield, oneHandRanged, etc.}, else if Type.armor, SubType enum {helm, cuirass, greaves, etc}.

I understand the basics, just finished my first semester of college Scripting in Unity, I'm just not sure doing this is as simple as I think it should be. Any assistance from Coding veterans would be greatly appreciated.

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
0

Answer by Maerig · May 13, 2014 at 08:51 AM

If you're talking about something like nested enums, this is not something you can do in C#. What you can do, however, is make a class for each of your enum entries (weapon, armor, etc), and make them inherit from the same class.

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 William_Weaver · May 13, 2014 at 10:20 AM 0
Share

Interesting. I'm looking up nesting enums right now. I'm better at UnityScript than C# presently, so it doesn't need to be C#. (updated thread title)

Unfortunately enums weren't covered in class, so this is one of many new functions I'm learning on my lonesome. I'll have to test it out and see how it works. It doesn't need to be an enum so long as the functionality works. I just liked the drop-down menu in the inspector for Type and want a SubType with options that are dependent on what Type is selected.

avatar image
0

Answer by snarf · May 13, 2014 at 10:26 AM

Instead of worrying too much about nest enums, why not make several distinct classes with different set of properties including type or socket type. Then you could make classes like Armor, Weapon, MiscItem, MagicItem, Potion etc.

What i'm trying to tell you: keep it simple :)

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 William_Weaver · May 13, 2014 at 10:29 AM 0
Share

That's a good idea actually. Rather than "Item" class, have each item have a variable : class that is it's type, then I can have a SubType enum that's specific to that type!

Only problem is my inventory system uses and Array of the Item class I have already. Haven't had much luck using arrays with multiple item types... Guess this'll be a good chance to get that array bit working.

avatar image snarf · May 13, 2014 at 11:13 AM 0
Share

If you're using generic arrays (which i strongly encourage you to do) you'd have the ability to add elements of other classes as long as the class inherits the defined class.

Anyway this is probably how I would do it:

 public class Item {
     // your item class here
 
     public string type;
     public string category;
 }
 
 public class Weapon : Item {
     // item is a weapon
 
     // set type and category strings
 }
 
 public class Armor : Item {
     // item is an armor
 
     // set type and category strings
 }
 
 using System.Collections.Generic;
 
 // example
 void Start ()
 {
     // ...
 
     List<Item> itemList = new List<Item>();
 
     itemList.Add(new Weapon("some sword", "blah", /* various stats, etc */));
 
     itemList.Add(new Armor("some cuirass", "blah", /* various stats, etc */));
 
 
 
     // ...
 }

again, hope this helps :)

avatar image William_Weaver · May 13, 2014 at 11:25 AM 0
Share

Thanks again for the help. I'll definitely give that a shot. If it works out I'll update this as the answer. I've been looking up the issues I had with generic arrays before, and it seems my issue was not type casting correctly. Out-dated reference material in the course text.. :\

(btw, love the username. ^_^)

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

22 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

Related Questions

Create an array of classes, of different type. 1 Answer

Setting up an array of Items 1 Answer

Making an item class in Unity. 1 Answer

Implementing an Item system - Defining Items 1 Answer

calling a class named by a string -1 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