• 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 Dec 15, 2012 at 07:25 PM by Dexter.Unity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dexter.Unity · Dec 14, 2012 at 08:44 PM · arrays

Accessing arrays by string name insted of integer index?

Hi guys!

As you told me before I should use Dictionary. However following the links you've provided I am not able to figure out how it works.

Do I make a 'Dictionary' in c# so my java script will work? Because the explanations on the links appears to be for c# even though I would like to use it for Javascript!?


The previous Question:

I am retrieving data from my localhost. Since Unity can only request a webpage is it only able to read webpage generated data as a String (Is this correct? I am not completely sure is an array also possible?).


So I made a script that allows me to read the Sting data and turn it into a array in Unity. The problem is that it seems that unity is not able to accept a string as array index. The following code gives me an error:

 function Awake()
 {
     var arr = new Array ();
     arr['test1'] = 'hi!';
     arr['test2'] = 'hi2!';
     Debug.Log(arr);
 }

Error: The best overload for the method 'Array.set_Item(int, Object)' is not compatible with the argument list '(String, String)'.


By not being able to use a string it also does not accept the following (which is the code I actually want to have working):

 function Awake()
 {
     var arr                 = new Array ();
     arr[0] = 'hi!';
     arr[1] = 'hi2!';
     Debug.Log(arr);
     
     var doesNotWorkEitherArray     = new Array ();
     doesNotWorkEitherArray[arr[0]] = 'hi3!';
     doesNotWorkEitherArray[arr[1]] = 'hi4!';
     
     
     Debug.Log(doesNotWorkEitherArray);
 }

Error: Cannot cast from source type to destination type.


Does some one know why this is not allowed and how to solve this problem (I really don't want to use [0] [1] as index for hp attackpower and so on... :(

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by DaveA · Dec 14, 2012 at 09:08 PM

Array is not the same as the javascript Array you may be used to. Use Hashtable or ArrayList instead. http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

Comment
Add comment · Show 10 · 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 Eric5h5 · Dec 14, 2012 at 09:10 PM 0
Share

I assume you meant Dictionary ins$$anonymous$$d of ArrayList?

avatar image Dexter.Unity · Dec 14, 2012 at 09:15 PM 0
Share

system.collections.hashtable.aspx is for C#?

avatar image DaveA · Dec 14, 2012 at 09:18 PM 0
Share

I have had luck with ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx) but yes, Dictionary is more suited to his desired syntax.

avatar image Eric5h5 · Dec 15, 2012 at 06:16 PM 1
Share

No, you use Dictionary in Unityscript. The syntax for generics is a bit different, namely

 var foo = new Dictionary<string, int>();

in C#, for example, would be

 var foo = new Dictionary.<String, int>();

in Unityscript. Everything else applies though. Remember to use "import System.Collections.Generic" at the top of your script so Dictionary and List can be used easily.

avatar image Eric5h5 · Dec 15, 2012 at 06:38 PM 1
Share

Read my last comment again please; you're using the C# syntax for generics rather than the Unityscript syntax. Also, neither 'test1' nor 0 are types, they are values. You need to supply the types that you're using in the Dictionary when you declare it; you add values later.

Show more comments
avatar image
0

Answer by gabs · Dec 14, 2012 at 09:18 PM

There's an easy way, if you're willing to do this in c#:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UnityAnswers_ParseData : MonoBehaviour
 {
     Dictionary<string,string> parsedData;
     
     void Start ()
     {
         // just a test, as if you received this from the server
         string dataFromServer = @"
 pies:delicious,
 tires:not so much,
 tree:green
 ";
         parsedData = new Dictionary<string, string>();
         // lets trim the data
         dataFromServer = dataFromServer.Trim();
         // and split
         string[] lines = dataFromServer.Split(',');
         foreach(string line in lines)
         {
             string[] keyValue = line.Trim().Split(':');
             if(keyValue.Length < 2)
                 continue;
             parsedData[keyValue[0]] = keyValue[1];
         }
         //
         //
         //
         //
         // NOW, to access the data:
         //parsedData["pies"] will output "delicious
         Debug.Log(parsedData["pies"]);
         Debug.Log(parsedData["tires"]);
         Debug.Log(parsedData["tree"]);
     }
 }
Comment
Add comment · Show 3 · 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 Dexter.Unity · Dec 14, 2012 at 09:26 PM 0
Share

C# Looks scary x) thanks for you script though!

avatar image Eric5h5 · Dec 14, 2012 at 10:02 PM 0
Share

You could write that just as well in Unityscript. It's not really anything to do with C#; you use the same datatypes and classes in either case.

avatar image gabs · Dec 14, 2012 at 10:05 PM 0
Share

Yeah. I just can't go back to the stone age, that's why.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

JesseEtzler's RPG Talent System in C# 1 Answer

Dungeon crawler problem(getting transform data from an array) 1 Answer

Array index is out of range 2 Answers

Can you help me with arrays? 2 Answers

Need help with converting an array to a list in C#` 4 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