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 adelphiaUK · Aug 14, 2014 at 10:08 PM · arrayslistspositioningmultidimensional array

Multidimensional Array of Vector2

Could someone please explain how I can make a multidimensional array of Vector2 variables.

Currently I am trying to use a list as follows:

     private List<Vector2[, , , , , ,]> enemySpawnPositions = new List<Vector2[, , , , , ,]>();
 

But my question is, how do I populate the data?

I have tried the following:

     enemySpawnPositions.Add(new Vector2[new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0)]);
     enemySpawnPositions.Add(new Vector2[]{new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0)});
     enemySpawnPositions.Add(new Vector2[{ new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) }]);
     enemySpawnPositions.Add(new Vector2[ Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0)]);
     enemySpawnPositions.Add(new Vector2[]{ Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0)});
     enemySpawnPositions.Add(new Vector2[{  Vector2(0, 0),  Vector2(0, 0),  Vector2(0, 0),  Vector2(0, 0),  Vector2(0, 0),  Vector2(0, 0),  Vector2(0, 0) }]);

...but all give me errors (which isn't surprising). So could someone please explain how I populate multidimensional arrays/lists. The data is irrelevant right now, I just need to know how to do it.

In case you're wondering I need a block of 7 (always 7) Vector2 co-ordinates but an unlimited quantity of them.

Thanks

Comment
Add comment · Show 1
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 IvovdMarel · Aug 14, 2014 at 10:14 PM 0
Share

Some simple examples:

$$anonymous$$aking a 1D array of Vector2 objects: new Vector2[];

$$anonymous$$aking a 2D array of Vector2:

vectorArray = new Vector2[7,7];

for (int i = 0; i < vectorArray.GetLength(0); i++) {

for (int j = 0; j < vectorArray.GetLength(1); j++) {

vectorArray[i,j] = new Vector2(0,0);

}

}

$$anonymous$$aking a 2D list of Vector2:

List> vectorList = new List>();

vectorList.Add(new List() { new Vector2(0,0), new Vector2(0,0).. etc

2 Replies

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

Answer by adelphiaUK · Aug 15, 2014 at 11:01 AM

OK. I've managed to sort it.

I was making it more complicated than I should have been.

My solution is:

     private List<Vector2[]> enemySpawnPositions = new List<Vector2[]>();
 

And then to populate it, it's:

     enemySpawnPositions.Add(new Vector2[7] { new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) });
 

Or if you don't care how many you have per group then it's:

     enemySpawnPositions.Add(new Vector2[] { new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) });
 

Note: the omission of 7 in the square brackets.

Thanks guys for pointing me in the right direction!

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
2

Answer by rutter · Aug 14, 2014 at 10:20 PM

Supposing you have a 2-dimensional array:

 int[,] values = new int[10,10];

You could access it like so:

 int x = 0;
 int y = 0;
 values[x,y] = 10;

With that said, though, your description at the bottom sounds more like this sort of structure:

 class MyClass {
     public Vector2 a; //give these vars descriptive names
     public Vector2 b;
     public Vector2 c;
     public Vector2 d;
     public Vector2 e;
     public Vector2 f;
     public Vector2 g;
 }

 List<MyClass> myList = new List<MyClass>();
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 adelphiaUK · Aug 15, 2014 at 09:20 AM 0
Share

I had tried this method but it becomes a bit impractical when you have to do the following to add one entry (remember, the entries needs to be unlimited):

 $$anonymous$$yClass insertion = new $$anonymous$$yClass();
 insertion.a = new Vector2(0,0);
 insertion.b = new Vector2(0,0);
 insertion.c = new Vector2(0,0);
 insertion.d = new Vector2(0,0);
 insertion.e = new Vector2(0,0);
 insertion.f = new Vector2(0,0);
 insertion.g = new Vector2(0,0);
 myList.Add(insertion);

Either that or I'm misunderstanding your answer.

avatar image Josh707 · Aug 15, 2014 at 10:30 AM 0
Share

You should be able to initialize a, b and so on as Vector2.zero in the constructor of $$anonymous$$yClass rather than writing it like that

avatar image adelphiaUK · Aug 15, 2014 at 11:44 AM 0
Share

I appreciate that, but Vector2(0,0) was just being used as an example. I may want to do Vector2(3.5f, 0.2f) or Vector2(6.3f, 1.5f) if you see my point. $$anonymous$$aybe i could have written a constructor within my class which would have accomplished the same thing I guess. Anyway, it's sorted now, thanks (see my answer).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple scripts vs arrays vs lists? 0 Answers

How do i convert this code snippet to a list? 1 Answer

Custom Inspector: For every new element in one array create a brand new array? 0 Answers

Inventory String error 1 Answer

How can I create a tree like list/array structure? 0 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