Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Dictionaries?

Discussion in 'Scripting' started by ByDefault_Studios, Aug 1, 2016.

  1. ByDefault_Studios

    ByDefault_Studios

    Joined:
    Jul 28, 2016
    Posts:
    9
    I'm trying to make a dictionary when I can easily find out values.

    In Lua you can do:

    local Blocks = {}
    Blocks[x][y][z] = "SomeValue"


    What is the C# equivalent to that? I'm new at C# and Unity (after coming from ROBLOX)
     
    hopetolive and levelingames like this.
  2. Diaonic

    Diaonic

    Joined:
    Jun 21, 2016
    Posts:
    56
    Code (CSharp):
    1. Dictionary<keytype, objectType> dictionaryName;
    2.  
    3. Dictionary <string, string> fruits = new Dictionary<string, string>();
    4.  
    5. fruits.Add("apple","macintosh");
     
  3. ByDefault_Studios

    ByDefault_Studios

    Joined:
    Jul 28, 2016
    Posts:
    9
    Are you able to to stuff like this with that code?

    fruit["apple"] = "Ripe";

    Debug.Log(fruit["apple"]); // Logs "Ripe"
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Yes.
     
  5. ByDefault_Studios

    ByDefault_Studios

    Joined:
    Jul 28, 2016
    Posts:
    9
    Would anyone mind giving me a working example of dictionaries in C#?
     
  6. Instalox

    Instalox

    Joined:
    Dec 2, 2015
    Posts:
    2
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Your problem might be that you're including System.Collections.Generic at the top (with a using statement).

    Hang on a sec, I'll stick something up here.

    Code (csharp):
    1.         Dictionary <string, string> fruits = new Dictionary<string, string>();
    2.         fruits.Add("apple","macintosh");
    3.         Debug.Log( fruits["apple"]);
     
    orionsyndrome likes this.
  8. ByDefault_Studios

    ByDefault_Studios

    Joined:
    Jul 28, 2016
    Posts:
    9
    I'm getting these errors:
    cs(9,17): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
    cs(11,19): error CS0178: Invalid rank specifier: expected `,' or `]'

    With this code:


    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class Inventory : MonoBehaviour {

    Dictionary<string, string> Test = new Dictionary<string, string>();

    Test.Add("Test1","Test2");

    Debug.Log(Test["Test1"]);

    }
     
  9. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    http://lmgtfy.com/?q=c#+tutorial#

    Copy pasting random snippets with no understanding of them isn't going to get you very far...

    But just because I'm a nice guy, the last two lines of your code need to go into a method.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you need to do some basic scripting tutorials.
     
    ericbegue likes this.
  11. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
  12. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;// this namespace is required to import dictionaries
    5.  
    6. public class dictionaryExample : MonoBehaviour {
    7.     public Dictionary<string, bool> myDictionary = new Dictionary<string, bool>();// declair key type and value type : <string, bool>
    8.     public Dictionary<string, string> myDictionary2 = new Dictionary<string, string>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         if (myDictionary.Count < 10) {// gets count on dictionary
    13.             string c = myDictionary.Count.ToString ();
    14.             myDictionary.Add (c, false); // adds to dictionary
    15.             myDictionary2.Add (c, c);
    16.         }
    17.         foreach (KeyValuePair<string, bool> i in myDictionary) { //you can use this  to return keys and values (key value pair)
    18.             Debug.Log (i.Key);// i.Key will rwtun the key for that KVP
    19.             Debug.Log(i.Value);// i.Value returns value fot the KVP
    20.         }
    21.         foreach (var i in myDictionary2) {// var can me used as a shortcut for many class types (in this case KeyValuePair <string, string>)
    22.             Debug.Log (i.Key);
    23.             Debug.Log(i.Value);
    24.         }
    25.         Debug.Log (myDictionary ["0"]);// this overload : myDictionary["0"]   will return the value associated to the Key : "0"
    26.         if (myDictionary.ContainsKey ("9")) {
    27.             myDictionary.Remove ("9"); // will remove a KVP
    28.             Debug.Log ("Has Removed");
    29.         }
    30.         myDictionary2.Clear ();// will clear the dictionary
    31.     }
    32. }
    33.  
     
  13. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Do you have a specific application for your dictionaries or just a general rundown of what they are? The C# page on .NET is pretty good at showing them off.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,668
    It's a spam bot.
     
    orionsyndrome likes this.
  15. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    I had a hunch given the links, maybe the bot wanted to learn about dictionaries? :p Cheers
     
    Agoston_R likes this.