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 Jordi · Jan 12, 2011 at 05:14 PM · hashtablecustom-class

Custom class and hashtable

I've been trying to set up my own class, GeneralItem, and store these in a Hashtable. The scripts are as follows:

GeneralItem class

using UnityEngine; using System.Collections;

public class GeneralItem : Object {

 public string itemName;
 public int sellPrice;
 public Texture icon;
 public bool isStackable;
 public string description;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }

}

ItemList script

using UnityEngine; using System.Collections;

public class ItemList : MonoBehaviour {

 public GeneralItem Turnip, Potato;
 private int amountOfItems;
 public GeneralItem[] itemArray;
 public Hashtable itemList = new Hashtable();
 //Hashtable: key is what you search, value is what is returned

 // Use this for initialization
 void Awake () {
     createItemList();
 }

 // Update is called once per frame
 void Update () {

 }

 private void createItemList() {

     Turnip = new GeneralItem();
     Turnip.itemName = "Turnip";
     Turnip.sellPrice = 50;
     Turnip.icon = Resources.Load("icons/ItemIcon") as Texture;
     Turnip.isStackable = true;
     Turnip.description = "Just a turnip.";
     itemList.Add(Turnip.itemName, Turnip);


     Potato = new GeneralItem();
     Potato.itemName = "Potato";
     Potato.sellPrice = 60;
     Potato.icon = Resources.Load("icons/PotatoIcon") as Texture;
     Potato.isStackable = true;
     Potato.description = "Only a potato.";
     itemList.Add(Potato.itemName, Potato);
 }

 public GeneralItem FindItemByName(string itemName) {
     if (itemList.ContainsKey(itemName)) {
         GeneralItem newItem;
         newItem = itemList["Turnip"] as GeneralItem;
         return(newItem);
     } else {
         return null;
     }
 }

}

However, when trying to see what value comes with "Turnip" in the hashtable, I get 'GeneralItem' instead of 'Turnip'. Also, when I try to assign a GeneralItem via the interface of Unity, neither Turnip nor Potato shows up in the list of usable GeneralItems.

My question is, how can I make sure 'Turnip' is retrieved in the hashtable instead of 'GeneralItem'?

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

1 Reply

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

Answer by yoyo · Jan 12, 2011 at 05:49 PM

A few suggestions and observations ...

  • when you say you "get 'GeneralItem'", do you mean that's what shows up in the Inspector? or are you printing the value? I suspect this is simply the default ToString method showing you the name of the type
  • if you want your items to show in the Inspector, you need to add the System.Serializable attribute to your class
  • you still won't see a drop down menu with Potato and Turnip, but you could add a custom editor that uses your hash table to do so
  • you might also add a ToString override method to GeneralItem to give you more information about the particular item
  • I suggest you use System.Collections.Generic, and then you can use Dictionary instead of simply Hashtable -- it gives you more type safety and ease of use, no need to typecast values retrieved from the dictionary
  • you probably don't need to derive from Object, just declare your GeneralItem class with no base class
  • GeneralItem.Start and GeneralItem.Update will never be called, since GeneralItem is not a component (it doesn't derive from MonoBehaviour) (I wish the Unity editor had right-click > Create Script and Create Behaviour as different menu items, with different starting code templates :-)
Comment
Add comment · Show 12 · 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 Mike 3 · Jan 12, 2011 at 06:06 PM 1
Share

If anything, you shouldn't ever derive from Object at all, as UnityEngine.Object is more for the base engine to use

avatar image Jordi · Jan 12, 2011 at 06:07 PM 0
Share

Thanks for the tips! I'll see em through script-wise. Just to get back to your initial question in the first point. I meant that Turnip and Potato do not show when I print itemList["Turnip"] as GeneralItem.

avatar image yoyo · Jan 12, 2011 at 06:22 PM 0
Share

Debug.Log(itemList["Turnip"]) is equivalent to Debug.Log(itemList["Turnip"].ToString()), and the default System.Object.ToString method just returns the name of the class. You could override it to return itemName, or more info about all the properties of your class.

avatar image yoyo · Jan 12, 2011 at 06:23 PM 0
Share

@$$anonymous$$ike, good idea -- I almost wrote that you can't do that in Unity3, but of course that's not one of the newly sealed classes. When you create a new script in the editor it gives you a template that derives from $$anonymous$$onoBehaviour, and if you're new to inheritance I can see how you would want to either leave $$anonymous$$onoBehaviour or replace it with another class. But yes, in this case, no base class is needed.

avatar image Jordi · Jan 13, 2011 at 08:04 PM 0
Share

And what I meant by not showing up in the Inspector: If I were to create a Public Texture , it would show up in the Inspector and I could visually choose a texture. I wanted the same for my GeneralItems. Also, can I school classes that inherit from GeneralItem still under GeneralItem (as in, when using that Dictionary)?

Show more comments

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

No one has followed this question yet.

Related Questions

What am I doing wrong with hashtable.CopyTo? 1 Answer

Convert returning object to Hashtable 1 Answer

creating and manipulating a grid efficiently 1 Answer

Problems with Hashtables and creation order? 2 Answers

Converting String to Variable Name 3 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