Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by PaxForce · Nov 02, 2014 at 10:59 AM · arraysnamenaming

how return array name in array

Hi all, I have an array of arrays - I believe it's called jagged array, but I'm not sure. How can I access or return the NAMES of arrays within that array? Not the values. I would like to print out the array names.

 string[] groupA = new string[]{name1, name2, name3, name4, name5};
 string[] groupB = new string[]{name6, name7, name8, name9, name10};
 string[] groupC = new string[]{name11, name12, name13, name14, name15}; 
 string[] groupD = new string[]{name16, name17, name18, name19, name20};
 string[] groupE = new string[]{name21, name22, name23, name24, name25};
 
 strging[][] groups = new string[][]{groupA, groupB, groupC, groupD, groupE};

I would like to get for example the third item in groups array by name: So that when I do this:

 Debug.Log(groups[2][]);

I would like the program to print out "group C". Not the items in group C.

Comment
Add comment · Show 3
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 Unitraxx · Nov 02, 2014 at 11:16 AM 0
Share

What do you mean by names of arrays? $$anonymous$$eys? I assume you are working with unityscript then?

avatar image PaxForce · Nov 02, 2014 at 11:32 AM 0
Share

Hi Unitraxx - I updated my question. PS: I don't know what "keys" are.

avatar image CanisLupus · Nov 02, 2014 at 12:53 PM 0
Share

@PaxForce C#'s reflection techniques might make it possible to print the name of a variable, but as soon as you insert groupC into groups it becomes just another unnamed array inside of an array of arrays. And don't even bother researching reflection, as it's not supposed to be used for "normal" program$$anonymous$$g. What you're trying to achieve is much better suited by Dictionaries (mapping a string name to an array) or Unitraxx's suggestion of $$anonymous$$ey/Value pairs.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Unitraxx · Nov 02, 2014 at 12:03 PM

I would look to use List or Dictionary. One suggestion:

 List<KeyValuePair<string, string[]>> groups = new List<KeyValuePair<string, string[]>>();
 groups.Add(new KeyValuePair<string, string[]>("Group A", new string[]{name1, name2, name3, name4, name5});
 groups.Add(new KeyValuePair<string, string[]>("Group B", new string[]{name6, name7, name8, name9, name10});
 groups.Add(new KeyValuePair<string, string[]>("Group C", new string[]{name11, name12, name13, name14, name15});


If you would like to print the group name now, you can do:

 Debug.Log(groups[2].Key);

If you want to access a group you can do:

 Debug.Log(groups[2].Value);

If you would like to access a name in a group:

 Debug.Log(groups[2].Value[index]);

Comment
Add comment · Show 5 · 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 PaxForce · Nov 02, 2014 at 12:22 PM 0
Share

I want to learn about $$anonymous$$eys and stuff one day, but right now I need something else. Please tell me how to do it using the script as I have it set up now.

avatar image Unitraxx · Nov 02, 2014 at 12:31 PM 0
Share

It's simply not possible.

avatar image PaxForce · Nov 02, 2014 at 12:35 PM 0
Share

really? I would never thought that such simple task would be impossible in C#. I'll try to check out the $$anonymous$$eys in some tutorial and than I'll get back to your answer and accept it if it works.

avatar image Unitraxx · Nov 02, 2014 at 12:35 PM 0
Share

And there's nothing special about the $$anonymous$$ey stuff. A $$anonymous$$eyValuePair is simply 2 values combined. With $$anonymous$$ey being the first value, and Value being the second value. (Similar to a Tuple, but limited to 2 values.)

avatar image Unitraxx · Nov 02, 2014 at 12:36 PM 0
Share

Well it's just not possible with solely the arrays you are using.

avatar image
0

Answer by Gardosen · Nov 02, 2014 at 01:25 PM

First of all, i would like to tell you that this way of using arrays is extremly ineffective. i have to agree with Unitraxx that his solution is a better one.

but if you wanna do it realy with our static sourcecode a solution would be the following.

Just add another array to your sourcecode

 string[] groupNames = { "groupA", "groupB", "groupC", "groupD", "groupE"};

with this help array, you can use the index for grabbing the array from the toparray and gather informations, or you are using the index on the groupNames array to get the name of the array you defined.

Example:

 int indexValue = 2;
 int groupId = 1;
 string groupAValue = groups[groupId][indexValue];
 string groupName = groupNames[groupId];
 
 Debug.Log("Groupname:"+groupName+" / Value:"+groupAValue);

the result would be "Groupname:groupA / Value:a Value"

but i realy want to mention again, you should change soon to the keypair value solution.

Greetings Gardosen

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a number/brackets in a function name? 1 Answer

Textmesh Username for networkplayers. help. 1 Answer

generating unique names for prefabs within an array 2 Answers

Naming array elements 1 Answer

naming a variable with a number 1 Answer


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