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 TrustBird · Aug 21, 2020 at 02:04 AM · dictionary

Dictionary Contains Key (TKey)

It works, but I wanted to make sure, if there is a better way to do, I'm still learning :)

thanks.

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEngine.XR.WSA;
 
 public class ElementData
 {   
        public int test;
 
  public ElementData(int e)
        {
            test = e;
        }
     
 }
     public class Test : MonoBehaviour
     {
     
     privat int complex;
     public int id ; // only for tested
 
     public Dictionary<int,ElementData> testDic = new Dictionary<int, ElementData>()
     
     void Start()
     {
             testDic.Add(1,new ElementData(600));
             testDic.Add(2,new ElementData(700));
             testDic.Add(3,new ElementData(800));
             testDic.Add(4,new ElementData(900));
             testDic.Add(5,new ElementData(1000));
     }
     
     
     void Update()
     {
               if (Input.GetKeyDown(KeyCode.K))
               {
                        complex = Random.Range(6,15);
     
                        if (!testDic.ContainsKey(complex))
                        {
                         CreatDictionar(complex);
                         } else {
                         complex = Random.Range(100,200);
                         CreatDictionar(complex);
                         }
     
                }
     
     }
     
                 private void CreatDictionar(int n)
                 {
                 testDic.Add(n, new ElementData(id));
                }
 }
 








Comment
Add comment · Show 2
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 Captain_Pineapple · Aug 21, 2020 at 12:00 PM 0
Share

Better way to do what? What is the point/goal of your code? What is the question?

avatar image TrustBird Captain_Pineapple · Aug 22, 2020 at 11:25 AM 0
Share

I want to make one list of all objects representing a character, and then check the parameters of these characters.

What are the disadvantages of dictonary, and whether it is a good use for dicsonary

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TrustBird · Sep 26, 2020 at 09:04 AM

I'm trying to make a strategy game, where in dictonary I keep all the parameters of the character, there should be many of them, but they are only representations of the character as an icon and a number.

this gives me access to all characters from one place but i need to know the dictonary key

this method seems quick to me, but less object-oriented (I don't know if I'm right)

Is this approach ok or it is better to do it differently. I don't know if I explained it well enough

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

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

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

Searching through a text file with Unity? 4 Answers

[JS] String,List dictionary doesn't compile. Any suggestions? 1 Answer

A new expression requires () or [] after type 1 Answer

Hashtable(or generic dictionary) vs Enum - when do I use which? 1 Answer

Dictionary reference disappearing 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