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 bonesbrigade · Nov 17, 2012 at 10:26 PM · foreach2d arraybuiltin array

Foreach / For In with a 2D array

I am using a 2D array of my GameObject type 'Unit'.

Basically I am doing this:

 var maxUnits:int = 25;
 private var teams:Unit[,];
 ...
 function Awake(){    
     // do other stuff
     ...
     teams = new Unit[2, maxUnits];
 }
 ..
 private function SpawnUnits(count:int) {
     for (var i = 0; i < count; i++)    {
         // do stuff here
         ...
         var unit:Unit = Unit.SpawnUnit();
         // more stuff
         teams[team, i] = unit;
     }
 }

And then later I attempt to run a foreach or for in on this (haven't been able to find syntax for this on a 2D array) and am not sure which one to use:

 function ChangeTurn() {
     currentTeamTurn = (currentTeamTurn == 0 ? 1 : 0);
     
     // reset active team's units
     foreach(u:Unit in teams[currentTeamTurn]){            
         u.Reset();
     }
 }

I have looked around on the forums and answers board quite a bit, and even looked at some .NET docs and this wiki page about Arrays.

But cannot find the answer - can anyone help me out?

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 bonesbrigade · Nov 17, 2012 at 09:08 PM 0
Share

I am currently just invoking the array with a normal for loop, which fits the bill in the sense that it is allowing me to find every object I need.

As far as being able to optimize my code, I would like to change this to a foreach type loop.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Nov 17, 2012 at 11:11 PM

You can't do a foreach on a 2d array - the code you've written is implying that it is a jagged array (an array of arrays) which would be defined, I believe, like this:

 public var myJaggedArray : int[][];

 public var jagged = [ [0,0,0,0], [1,1,1,1], [2,2,2,2] ];

If you want a 2D array - then what you have is fine. If you really want to be able to do a foreach then you should redefine it as a jagged array or a single dimensional array and work out the logical X,Y within it.

I would suggest using List. so that you can actually incrementally add things to each team. Doing it that way:

 public var teams : List.<List.<Unit>> = new List.<List.<Unit>>();

There's more information on growing collections here

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 bonesbrigade · Nov 18, 2012 at 08:47 AM 0
Share

Yes I looked into using Lists - but couldn't find any docs regarding their use with a multi-dim array.

I tried your code above and get this error: expecting >, found '>>'.

Any ideas?

[Also - thanks for that link!]

avatar image whydoidoit · Nov 18, 2012 at 09:37 AM 0
Share

Ugh you are right, looks like JS can't actually make a generic list of a generic list (why I don't know - .NET can do it!)

avatar image whydoidoit · Nov 18, 2012 at 09:37 AM 0
Share

You'd have to do it like this -

 #pragma strict
 
 class Unit
 {
 }
 
 class Units
 {
     var units : List.<Unit> = new List.<Unit>();
 }
 
 
 public var listOfLists = new List.<Units>();
 
 function Start () {
     listOfLists.Add(new Units());
     listOfLists.Add(new Units());
     listOfLists[0].units.Add(new Unit());
     listOfLists[1].units.Add(new Unit());
 }

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

10 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

Related Questions

How to set a value for each level without using a switch statement 2 Answers

StartCoroutine for each object in a foreach() Loop 1 Answer

Change global state in list 1 Answer

Foreach - what am I doing wrong? 1 Answer

Getting all objects with a tag and inserting them into an array of gameobject 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