Search Unity

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

Discussion in 'Scripting' started by kavehmb2000, Sep 9, 2015.

  1. kavehmb2000

    kavehmb2000

    Joined:
    Aug 6, 2015
    Posts:
    2
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public int p1Score = 0;                    // The player1's score.
    9.     public int p2Score = 0;                    // The player2's score.
    10.     private int maxScore = 10;
    11.     Dictionary<string,Text> myScores = new Dictionary<string,Text >();
    12.  
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         GameObject canvas = GameObject.Find("Canvas");
    18.         Text[] texts = canvas.GetComponentsInChildren<Text>();
    19.         myScores = GetTextObjects (texts);
    20.     }
    21.  
    22.  
    23.     void Update ()
    24.     {
    25.         // Set the score text.
    26.         myScores["P1Score"].text = "Player 1: " + p1Score.ToString ();
    27.         myScores["P2Score"].text = "Player 2: " + p2Score.ToString ();
    28.      
    29.         // If a player wins
    30.         //if((p1Score == maxScore)||(p2Score == maxScore))
    31.             // ... end the game
    32.  
    33.     }
    34.     private Dictionary GetTextObjects(Text[] txt)
    35.     {
    36.  
    37.         Dictionary txtDic = new Dictionary <string, Text>();
    38.         txtDic.Add ("P1Score", txt[0]);
    39.         txtDic.Add ("P2Score", txt[1]);
    40.         return txtDic;
    41.     }
    42.  
    43. }
    44.  
    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. ;)
    Thanks in advance,
    Kaveh
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    kavehmb2000 likes this.
  3. kavehmb2000

    kavehmb2000

    Joined:
    Aug 6, 2015
    Posts:
    2
    Thanks, it did resolve my problem :D
     
  4. Master109

    Master109

    Joined:
    Aug 25, 2013
    Posts:
    2
    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?