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 Mikael-Madrid · Jun 30, 2013 at 06:42 PM · c#inventorydictionary

Item Database / Dictionary

So basically,what I'm trying to accomplish is create a dictionary and populate it so i can access the items to generate them.

 using UnityEngine;
 using System.Collections;
 
 public class ItemClass : MonoBehaviour {
 
     public string name;
     public string description;
     public int id;
     public int uses;
     public tk2dSpriteAnimator animations;
     
     public ItemClass(string name,string description,int id,int uses,tk2dSpriteAnimator animations)
     {
         
         this.name = name;
         this.description = description;
         this.id = id;
         this.uses = uses;
         this.animations =animations;
             
     }
 }
 
 public class ItemsDatabase : MonoBehaviour {
     
     public Dictionary <string, ItemClass> dictionary = new Dictionary<string, ItemClass>();
  
     public ItemClass metal = new ItemClass("Metal","Muy Duro,Brilla",0,1,null);
     dictionary.Add(metal.name,metal);
     
     
 }

the dictionary.Add(metal.name,metal); line fails,any insight?,thanks in advance.

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

Answer by Seregon · Jun 30, 2013 at 06:51 PM

Looking at the code posted, it looks like the dictionary.Add(metal.name,metal); line is inside the class definition, not inside a function. You can't run code inside a class definition, only define functions and member variables (in c# atleast, I think js is different). It doesn't look like theres anything wrong with the rest of the code, so the following should work:

 public class ItemsDatabase : MonoBehaviour {
  
 public Dictionary <string, ItemClass> dictionary = new Dictionary<string, ItemClass>();
  
 public ItemClass metal = new ItemClass("Metal","Muy Duro,Brilla",0,1,null);
 
 public void Start()
    {
       dictionary.Add(metal.name,metal);
    }
  
 }

You may or may not also want the dictionary and metal initialisations in the Start() function, but they will work equally well outside a function definition.

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 Mikael-Madrid · Jun 30, 2013 at 07:05 PM 0
Share

Thank,Im such a dumbass I didn't realize that, I also had to add using System.Collections.Generic; Cheers mate :)

avatar image Seregon · Jun 30, 2013 at 07:13 PM 0
Share

No worries, it's easy to miss the obvious errors - I didn't spot the missing using statement either...

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

16 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

Related Questions

Can't access singleton's dictionary from another singleton 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Accessing a Class's Variables from within a Dictionary Element 1 Answer

How can I delete an item from this inventory system? 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