C# : The type or namespace name `Dictionary' could not be found

Hi,
I’m trying to create a script to update my scores (two TEXT objects, not GUIText).
I tried to use a Dictionary to call these TEXT objects by name.
here is my code:

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

public class Score : MonoBehaviour
{
    public int p1Score = 0;                    // The player1's score.
    public int p2Score = 0;                    // The player2's score.
    private int maxScore = 10;
    Dictionary<string,Text> myScores = new Dictionary<string,Text >();

  
  
    void Start()
    {
        GameObject canvas = GameObject.Find("Canvas");
        Text[] texts = canvas.GetComponentsInChildren<Text>();
        myScores = GetTextObjects (texts);
    }
  
  
    void Update ()
    {
        // Set the score text.
        myScores["P1Score"].text = "Player 1: " + p1Score.ToString ();
        myScores["P2Score"].text = "Player 2: " + p2Score.ToString ();
      
        // If a player wins
        //if((p1Score == maxScore)||(p2Score == maxScore))
            // ... end the game

    }
    private Dictionary GetTextObjects(Text[] txt)
    {

        Dictionary txtDic = new Dictionary <string, Text>();
        txtDic.Add ("P1Score", txt[0]);
        txtDic.Add ("P2Score", txt[1]);
        return txtDic;
    }
  
}

The problem is that I get the following error on Unity (ver. 5.1.2f1):
Score.cs(34,17): error CS0246: The type or namespace name `Dictionary’ could not be found. Are you missing a using directive or an assembly reference?

I think I have declared using System.Collections.Generic namespace. What am I doing wrong?

Note: I have simplified the code here, I know I should check for Text[ ] length or it being non-null before using it. or using exception handling or other measures. :wink:
Thanks in advance,
Kaveh

Line 34. There is no such type as a dictionary. You need to type return value to a Dictionary <SomeType, SomeOtherType>.

There are other ways to appoach this as well, like using a generic or returning an interface.

1 Like

Thanks, it did resolve my problem :smile:

I’m having the same kind of problem, but it’s with KeyValuePair<TKey, TValue>. This is strange because I have been using KeyValuePairs pairs in the past. Maybe it has something to do with the fact that I’m using the 2019 beta?