Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by James Simpson · May 10, 2010 at 09:41 PM · arraysdynamic

Dynamic multi-dimensional array in C#?

I need to be able to create an array that stores the userid of all users in a room with the current x, y, z position that they are in. I assume I will need to use ArrayList, but I'm not sure how to go about setting it up so that this will work like I'm thinking.

Example:

[0] => user1
  [0] => 20
  [1] => 25
  [2] => 15
[1] => user2
  [0] => 27
  [1] => 25
  [2] => 65
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mike 3 · May 10, 2010 at 10:50 PM

You could alternatively go with a Dictionary or Hashtable - both of them are similar, though Dictionary is the Generic (more strictly typed) version

The idea is that you store a key value pair, in this case the key would be the userid, the value would be the vector position

You can then use the userid to get/set the value easily

example:

Dictionary users = new Dictionary(); users["bob"] = new Vector3(1,2,3); users["jeff"] = Vector3.zero;

Debug.Log(users["bob"]); //this would print <1,2,3>

and example for hashtable:

Hashtable users = new Hashtable(); users["bob"] = new Vector3(1,2,3); users["jeff"] = Vector3.zero;

Debug.Log(users["bob"]); //this would print <1,2,3>

although it looks like hashtable has less to type here, it's not always the case - you often have to cast the output from it

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

Answer by Michael La Voie · May 10, 2010 at 10:01 PM

As long as you're not developing for the iPhone, you'll want to use the generic "List". Otherwise, Arraylist is fine.

The syntax for both is pretty similar:

List<Vector3> myVecs = new List<Vector3>(); myVecs.Add(new Vector3(0.5f, 1.0f, 0.3f));

Vector3 firstVec = myVecs[0];

Or

ArrayList myVecs = new ArrayList(); myVecs.Add(new Vector3(0.5f, 1.0f, 0.3f));

Vector3 firstVec = (Vector3)myVecs[0];

The big difference is with generics (like List<>) you can specify the data type that it holds and it will enforce that. It can save you from making stupid mistakes and speeds up processing because it always knows what its working with.

ArrayLists always hold the "Object" data-type. Fortunately, everything derives from that type, so you're always set. You can just add anything to an ArrayList. Of course, then you suffer from "boxing" and "un-boxing" slowdowns, which is just a fancy way of saying the slowdown that happens when your data is converted to a generic "Object" when its added, then back to the actual data type (in this case Vector3) when you need it.

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 Eric5h5 · May 10, 2010 at 10:21 PM 0
Share

You can't use doubles with Vector3, they have to be floats.

avatar image Michael La Voie · May 10, 2010 at 10:22 PM 0
Share

Thanks @Eric5h5, I've posted the fix

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

No one has followed this question yet.

Related Questions

How to set color to gameObject respective to below coding? 2 Answers

Ways to minimize Object.stelemref() calls (low level) 1 Answer

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


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