Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Feb 14, 2017 at 01:34 AM by ZachRoman121 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by ZachRoman121 · Feb 12, 2017 at 01:19 AM · scriptableobjectdictionary

Add every ScriptableObject to a dictionary?

Hi. I am trying to make an item system. I got the inventory down. And i am making a way to get any item using a variable attached to all ScriptableObjects called ID. And i want to be able to find the ScriptableObject that has the ID. And the best way to do it that i found was to use a Dictionary. Here's my ScriptableObject script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu]
 public class Items : ScriptableObject {
 
     public Sprite itemSprite;
     public string itemName;
     public int itemId;
 }

But i cannot add the item to a Dictionary inside the ScriptableObject class. So. Any idea on how to do this? The dictionary i am trying to add the item to takes an Int and a String. The int is the ID. And the string is the name

Comment
Add comment · Show 1
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 Chikari · Feb 12, 2017 at 05:29 PM 0
Share

What's the problem with adding a string and an int to a dictionary within a scrip$$anonymous$$ble object? Please provide more code. :) Where is your dictionary set up and how are you trying to add to it?

1 Reply

  • Sort: 
avatar image
0

Answer by RobAnthem · Feb 12, 2017 at 06:04 PM

I wouldn't use a dictionary, as it isn't much faster than a simple

         public List<Item> ItemsList;
         public Item GetItem(int ID)
         {
             return ItemsList.Find(item => item.ID == ID);
         }

Now if your problem is adding the objects, then you need to make sure you are creating instances, and if you are doing so in the editor, you will need a custom editor window or inspector to create them. You can code up an inspector for the ItemDatabase class you make, then have inside your inspector a way to create and save the assets.

Like this

 public int IDs;
 public void CreateNew()
 {
    Item tempItem;
    AssetDatabase.CreateAsset(tempItem = CreateInstance<Item>(), "Assets/Resources/Items/" + "Item_" + IDs++.ToString() + ".asset");
    ItemsList.Add(tempItem);
 }

or

 public int IDs;
 public void Init()
 {
    AssetDatabase.CreateAsset(CreateInstance<ItemDatabase>(), "Assets/Resources/" + "ItemDatabase" + ".asset");
 }
 public void AddNew()
 {
    AssetDatabase.AddObjectToAsset(CreateInstance<Item>(), "Assets/Resources/ + "ItemDatabase" + ".asset");
 }


After the object exists, in your OnGUI of your custom inspector you can easily have an expanding list by saying.

 void OnGUI()
 {
        ItemDatabase Items = target as ItemDatabase;
        EditorGUILayout.BeginVertical();
        for (int i = 0; i < Items .ItemsList.Count; i++)
        {
               if (Items .ItemsList[i] != null)
               {
                      Items .ItemsList[i] = (Item)EditorGUILayout.ObjectField("Item", Items .ItemsList[i], typeof(Item), false);
               }
        }
        if (GUILayout.Button("Add New")
        {
               Items.AddNew();
        }
        EditorGUILayout.EndVertical();
 }
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 ZachRoman121 · Feb 12, 2017 at 09:28 PM 0
Share

I'm still confused. Is there a more simple way of doing that?

avatar image lucasmontec · Oct 18, 2020 at 09:28 PM 0
Share

A dictionary is $$anonymous$$UCH $$anonymous$$UCH faster than searching all elements in a list. A dictionary search is O(1) and a list search is O(N). This means that if your list contains 10000000 entities, it will have to check every single element in the worst case. Still, it is not likely that he is going to have thousands of elements anyway.

Follow this Question

Answers Answers and Comments

67 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 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 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

How to use ScriptableObject for Inventory System 0 Answers

Serializing Dictionary: Data lost between sessions 0 Answers

[Solved]How to serialize Dictionary with Unity Serialization System 6 Answers

Dictionaries don't serialize within scriptable objects 0 Answers

please help me, Scriptable object's variable serialization issue 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