Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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,843
    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.