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 BadAssGames · Nov 15, 2013 at 06:46 AM · variablessort

C# - How do I access a variable on a passed Type?

What I'm trying to do is a pass a Type as a variable, then access a variable from the passed type. I have several classes which are subclasses of another.

My base class is called InventoryItem, its two subclasses look like this:

 public class Animal : InventoryItem
 {
     public enum CategoryType {PIG = 0, CAT, NUM_CATEGORYTYPE}
     public CategoryType categoryType = CategoryType.PIG;
 }


 public class Utility : InventoryItem
 {
     public enum CategoryType {POTION = 0, FENCE, ROAD, NUM_CATEGORYTYPE}
     public CategoryType categoryType = CategoryType.POTION;
 }

I want to create a sorted list from another script. In this script, inventoryList is a list of all the inventoryItems (these can be Animal's whose CategoryType is PIG or CAT, or they can be Utilities whose CategoryType is whatever). The goal of the function is to grab all the pigs and make a list out of them.

The function for this is something like:

 InventoryItem[] inventoryList = new InventoryItem[20];
 
 //classType refers to the class (either Animal or Utility)
 //sortType refers to the enumerator
 //The goal of the script is to return a list of Animals that are pigs
 CreateSortedList<classType>(int sortType)
 {
    if(sortType == (int)classType.CategoryType.PIG)
    {
      print("Looking for pigs");
    }
 }



However, the compiler tells me that CategoryType is not a part of classType(because it doesn't know that classType can be an Animal or Utility).

What am I doing wrong here?

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
Best Answer

Answer by BadAssGames · Nov 15, 2013 at 08:37 AM

I came up with "A" solution. I welcome any other suggestions. It's a little sloppy, but it will work for my purposes. GetEnumerator() is a virtual function on the base class InventoryItem. Then I override it on all the subclasses. It just returns their categoryType as an int.

 InventoryItem[] CreateSortedList<classType>(int sortType) where classType : class
         {
             System.Collections.Generic.List<InventoryItem> totalList = new System.Collections.Generic.List<InventoryItem>();//the totalList of classType
             System.Collections.Generic.List<InventoryItem> subList = new System.Collections.Generic.List<InventoryItem>();//the sublist of classType
             
             for(int i = 0; i < inventoryList.Length; i ++)
             {
                 if(inventoryList[i] is classType)//If this is the classType we're looking for
                 {
                     totalList.Add (inventoryList[i]);//Add it to the totalList of classType
                     
                 }
             }
             
             for(int i = 0; i < totalList.Count; i ++)
             {
                 if(totalList[i].GetEnumerator() == sortType)//For every item in our totalList, get the enumerator and compare it to sortType
                 {
                     subList.Add (totalList[i]);
                 }
             }
     
             return subList.ToArray();
         }
 
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 zanearn · Nov 15, 2013 at 07:35 AM

Can you put all these types into a single namespace (e.g. MyFarm)? If so, you can then using that namespace (using MyFarm) in classes that needs those types, and avoid future confusion.

OR you can simply put CategoryType in InventoryItem.

 public enum CategoryType {PIG = 0, CAT, POTION, FENCE, ROAD, NUM_CATEGORYTYPE}

--Edit--

I think you should then clearly name these types AnimalCategoryType and UtilityCategoryType.

Otherwise (int)classType.CategoryType.PIG == (int)classType.CategoryType.POTION

 if(sortType == (int)classType.CategoryType.PIG) // also true for classType.CategoryType.POTION
 {
      print("Looking for pigs"); // and potions
 }

Avoid making traps for yourself.

Comment
Add comment · Show 4 · 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 BadAssGames · Nov 15, 2013 at 07:43 AM 0
Share

Thanks for your answer, but I wanted to avoid having to put CategoryType in inventory item.

avatar image zanearn · Nov 15, 2013 at 08:02 AM 0
Share

When you call the above function for POTIONs, PIGs are angry too.

avatar image BadAssGames · Nov 15, 2013 at 08:10 AM 0
Share

They were named that way initially, actually. I renamed them to 'categoryType' universally because I thought that might solve my problem. I was wrong.

avatar image zanearn · Nov 15, 2013 at 08:24 AM 0
Share

Actually I'm making a similar thing as yours, and I put all types categories into a single namespace. Not sure if this is the best way of doing it, so will also be glad to see a better solution to this.

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

18 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

Related Questions

Sorting variable names by their values. 2 Answers

Find gameObject with higher variable 1 Answer

When I try to increment a variable by 1, why does it add 10? 1 Answer

Best way to handle a lot of variables 3 Answers

Can I have a private Variable "look" in folder? 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