Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Associative arrays

Discussion in 'Scripting' started by d@mmi, May 3, 2008.

  1. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    In Unity, Is there a way I can set up either associative arrays

    Code (csharp):
    1. value{key} = float
    or

    just as well, can I explicity define multi-dimensional arrays like

    Code (csharp):
    1. value[xInt][yInt] = float
     
  2. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    890
    If you're using Javascript, then you can't use multi-dimensional arrays. C# does support them though.

    For an associative array, use Mono's hashtable: http://msdn.microsoft.com/en-us/library/system.collections.hashtable(VS.71).aspx

    If you were using C#, you could use a Dictionary also.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,780
    Code (csharp):
    1. var foo = new Hashtable(); // Or "var foo = {};" if you want to be really terse
    2. foo["something"] = "blah";
    You can use multi-dimensional arrays in Javascript.

    Code (csharp):
    1. var foo = new Array(10);
    2. foo[0] = new Array(10);
    3. foo[0][0] = "blah";
    You can even use multidimensional built-in arrays in Javascript:

    Code (csharp):
    1. var tdata = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 32, 32);
    2. tdata[10, 25] = .75;
    You just can't define them from scratch, apparently, for some reason.

    --Eric
     
  4. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    Thanks a lot for your advice.

    I'll give it a go.