• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 02, 2014 at 07:48 AM by whydoidoit for the following reason:

Duplicate Question

avatar image
0
Question by NutellaDaddy · Mar 02, 2014 at 06:19 AM · dictionary

A new expression requires () or [] after type

I keep on getting the above error when I try to declare the Dictionary. How can I fix this?

 sing UnityEngine;
 using System.Collections.Generic;
 
 public class inventoryV1 : MonoBehaviour {
     public static bool inventoryOn = false;
     //GUI FONT AND SIZE
     private Vector2 windowPosition = new Vector2(300,0);
     private Vector2 windowSize = new Vector2(745, 750);
     
     private Vector2 closePosition = new Vector2(705,9);
     private Vector2 closeSize = new Vector2(24,24);
 
     private Vector2 inventoryTitlePosition = new Vector2(245,11);
     private Vector2 inventoryTitleSize = new Vector2(125,50);
 
     private Vector2 playerTitlePosition = new Vector2(17,11);
     private Vector2 playerTitleSize = new Vector2(100,50);
 
     private Vector2 craftingTitlePosition = new Vector2(17,414);
     private Vector2 craftingTitleSize = new Vector2(125,40);
 
     //Dictionary
     static public Dictionary<int, string> inventoryNameDictionary;
 
     //WINDOW TEXTURES
     public Texture inventoryWindow;
     
     //BUTTON TEXTURES
     public Texture closeInventory;
     public Texture inventorySlot;
 
     //ACCESS CLASSES
     ItemClass itemObject = new ItemClass();
 
 
     void Update () 
     {    
         //ON OR OFF
         if (Input.GetKeyUp (KeyCode.Tab)) 
         {
             if (inventoryOn == false) 
             {
                 inventoryOn = true;
             } 
             else if (inventoryOn == true) 
             {
                 inventoryOn = false;
             }
             if(Optionsmenu.isOptionsOn == true)
             {
                 inventoryOn = false;
             }
         }
     }
     //GUI AND FUNCTIONALITY
     void OnGUI()
     {
         if (inventoryOn == true) 
             {
              GUILayout.BeginArea(new Rect (windowPosition.x, windowPosition.y, windowSize.x, windowSize.y), inventoryWindow);
             //Close Button
             if (GUI.Button (new Rect (closePosition.x, closePosition.y, closeSize.x, closeSize.y), closeInventory)) {
                 inventoryOn = false;
             }
             //INVENTORY SECTION TITLES
             GUI.Label(new Rect(inventoryTitlePosition.x, inventoryTitlePosition.y, inventoryTitleSize.x, inventoryTitleSize.y),"Inventory");
             GUI.Label (new Rect( playerTitlePosition.x,  playerTitlePosition.y,  playerTitleSize.x, playerTitleSize.y), "Player");
             GUI.Label(new Rect(craftingTitlePosition.x, craftingTitlePosition.y, craftingTitleSize.x, craftingTitleSize.y), "Crafting Menu");
             //INVENTORY SLOTS
             //FIRST ROW
             GUI.Button (new Rect (270, 50, 70, 70), inventoryNameDictionary[0]);
             GUI.Button (new Rect (345, 50, 70, 70), inventoryNameDictionary[1]);
             GUI.Button (new Rect (420, 50, 70, 70), inventoryNameDictionary[2]);
             GUI.Button (new Rect (495, 50, 70, 70), inventoryNameDictionary[3]);
             GUI.Button (new Rect (570, 50, 70, 70), inventoryNameDictionary[4]);
             GUI.Button (new Rect (645, 50, 70, 70), inventoryNameDictionary[5]);
             //SECOND ROW
             GUI.Button (new Rect (270, 125, 70, 70), inventoryNameDictionary[6]);
             GUI.Button (new Rect (345, 125, 70, 70), inventoryNameDictionary[7]);
             GUI.Button (new Rect (420, 125, 70, 70), inventoryNameDictionary[8]);
             GUI.Button (new Rect (495, 125, 70, 70), inventoryNameDictionary[9]);
             GUI.Button (new Rect (570, 125, 70, 70), inventoryNameDictionary[10]);
             GUI.Button (new Rect (645, 125, 70, 70), inventoryNameDictionary[11]);
             //THIRD ROW
             GUI.Button (new Rect (270, 200, 70, 70), inventoryNameDictionary[12]);
             GUI.Button (new Rect (345, 200, 70, 70), inventoryNameDictionary[13]);
             GUI.Button (new Rect (420, 200, 70, 70), inventoryNameDictionary[14]);
             GUI.Button (new Rect (495, 200, 70, 70), inventoryNameDictionary[15]);
             GUI.Button (new Rect (570, 200, 70, 70), inventoryNameDictionary[16]);
             GUI.Button (new Rect (645, 200, 70, 70), inventoryNameDictionary[17]);
             //FOURTH ROW
             GUI.Button (new Rect (270, 275, 70, 70), inventoryNameDictionary[18]);
             GUI.Button (new Rect (345, 275, 70, 70), inventoryNameDictionary[19]);
             GUI.Button (new Rect (420, 275, 70, 70), inventoryNameDictionary[20]);
             GUI.Button (new Rect (495, 275, 70, 70), inventoryNameDictionary[21]);
             GUI.Button (new Rect (570, 275, 70, 70), inventoryNameDictionary[22]);
             GUI.Button (new Rect (645, 275, 70, 70), inventoryNameDictionary[23]);
             
             GUILayout.EndArea ();
             //ARMOR SLOTS
 
 
         //DICTIONARY
          Dictionary<int, string> inventoryNameDictionary = new Dictionary<int, string>()
             {
                             //THE ERROR IS RIGHT HERE
                 (0, string.Empty),
                 (1, string.Empty),
                  (2, string.Empty),
                 (3, string.Empty),
                  (4, string.Empty),
                 (5, string.Empty),
                  (6, string.Empty),
                 (7, string.Empty),
                  (8, string.Empty),
                 (9, string.Empty),
                 (10, string.Empty),
                  (11, string.Empty),
                 (12, string.Empty),
                  (13, string.Empty),
                 (14, string.Empty),
                 (15, string.Empty),
                 (16, string.Empty),
                 (17, string.Empty),
                 (18, string.Empty),
                 (19, string.Empty),
                 (20, string.Empty),
                 (21, string.Empty),
                  (22, string.Empty),
                 (23, string.Empty)
 
             };
 
         }
     
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by mattyman174 · Mar 02, 2014 at 06:25 AM

Your trying to declare it Twice. Once at the top and then again when you fill it at the bottom.

 //DICTIONARY
 inventoryNameDictionary = new Dictionary<int, string>()
 {
 //THE ERROR IS RIGHT HERE
 (0, string.Empty),

Remove the Dictionary declaration like above.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NutellaDaddy · Mar 02, 2014 at 06:36 AM 0
Share

It still says that Assets/Scripts/inventoryV1.cs(106,35): error CS1526: A new expression requires () or [] after type for (0, string.Empty and this is what my script looks like now.

          inventoryNameDictionary = new Dictionary<int, string>()
             {
                 (0, string.Empty),
                 (1, string.Empty),
                 (2, string.Empty),
                 (3, string.Empty),
                 (4, string.Empty),
                 (5, string.Empty),
                 (6, string.Empty),
                 (7, string.Empty),
                 (8, string.Empty),
                 (9, string.Empty),
                 (10, string.Empty),
                 (11, string.Empty),
                 (12, string.Empty),
                 (13, string.Empty),
                 (14, string.Empty),
                 (15, string.Empty),
                 (16, string.Empty),
                 (17, string.Empty),
                 (18, string.Empty),
                 (19, string.Empty),
                 (20, string.Empty),
                 (21, string.Empty),
                 (22, string.Empty),
                 (23, string.Empty)
 
             };
 
         }
     
     }
 }
avatar image mattyman174 · Mar 02, 2014 at 07:34 AM 1
Share

Replace all the Brackets around your $$anonymous$$ey/Value pairs with Curly Braces.

 {0, string.Empty},
 {1, string.Empty},
 ...
 ...
 ...

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dictionary vs Hashtable 1 Answer

Serializing data structures the Unity way. Possible? 1 Answer

Determine an index of a closest number in a database, from a user's input 1 Answer

Can be list stored in dictionary? 1 Answer

IL2CPP Dictionary deserialization issue 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges