- Home /
Simple Multidimensional Associative Array question.
I'm creating and indexing a multidimensional associative array like so:
arrayOfFruits["apples"] = {}
arrayOfFruits["apples"]["oranges"] = {};
I make some assignments later on:
arrayOfFruits["fuji"]["mandarin"] = 1;
Later on in the code, I try to go:
var a = "redDelicious"; var b = "florida";
if (arrayOfFruits[a][b] == 1) { // do something with fruits }
On the "if" line, I get a NullReferenceExeption: Object ref not set to instance or object.
I've tried:
if (arrayOfFruits[a][b] != null)
no dice. :(
I know this must be terribly simple.
Answer by rule62 · Feb 20, 2011 at 01:59 AM
I initialized all possible pairs of items in the 2D array to 0. This seems to have solved the problems.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105251im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multidimensional GameObject Arrays 1 Answer
Getting C# to access a javascript global Array and Components 1 Answer
Using AnimationCurve in List. or Builtin Arrays 2 Answers
Beginner : Random gameobject from array 2 Answers
Arrays declaration c# 3 Answers