Nerd For Tech
Published in

Nerd For Tech

Dictionaries in Unity

A dictionary is a generic collection that is used to store key-value pairs. They are similar to arrays but store two components. The first is the key and the second is the value. If you think about it as an English dictionary, the key would be the word and the value would be the definition.

Dictionaries are included using the System.Collections.Generic namespace. To define one, you write it as Dictionary<keytype, objectType>.

To add values to the dictionary you will use the Add() method.

To remove values you will use the Remove() method.

What is unique about dictionaries is that when you search them you will use the key. When the key is found the value will be returned.

An easy way to print both the key and the value is to use the LINQ library. This will allow you to use the ElementAt() method to print both the key and the value.

To print the whole list you can use a foreach loop.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store