Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 amandaadler · Feb 02, 2020 at 06:03 PM · 2dscripting problemerrorstructdatastructure

How to store a Transform[,] grid in an ArrayList?

I am new to Unity game dev and trying to expand my knowledge of data structs. I'm building a match-3 game, and I currently have a multi-dimensional ArrayList holding transform coordinates on a grid of x width and y height. I have game objects tied to these coordinates in world space, with the grid syncing the data back in game space. I am attempting to push these coordinates into the nested list (familyList) to be deleted pending 3 or more units being added to familyArrayList (seen below).

Here's the abridged code:

 private static Transform[,] grid = new Transform[width, height];
 public static ArrayList familyArrayList = new ArrayList();
 
 // Go through game grid line by line and add game objects from game space into family ArrayList
 for (int y = height - 1; y >= 0; y--)
         {
             for (int x = 0; x < width; x++)
             {
                 if (grid[x, y] != null)
                 {
                     // Add grid[x, y] to familyArrayList
                     familyArrayList.Add(grid[x, y]);
                     Debug.Log(familyArrayList[0]);

Output:

 Green Unit(Clone) (UnityEngine.Transform)

How can I get familyArrayList to properly story the grid coordinates and not the game object the grid coordinates are referencing? i.e. familyArrayList[0] == '3,4' Should I instead use a nested list? At a later point in the code, I want to clear the game object from the board in world space as well as set the grid[x,y] to null:

 // Loop through all family array and destroy game objects
 foreach (var x in familyArrayList)
 {
     Destroy(familyArrayList(x).gameObject); 
 }
 // Loop through all family array and set their values to null in grid as well
 foreach (var x in familyArrayList)
 {
     familyArrayList(x) = null;
 }

But I get errors like "Argument 1: cannot convert from 'object' to 'int'" and "Non-invocable member 'Piece.familyArrayList' cannot be used like a method" which I can imagine are explained by my above problem of not storing the grid coordinates properly and instead storing the referenced game object. I'm truly stumped on this problem and thank anybody in advance who can point me in the right direction to solving this.

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 sacredgeometry · Feb 02, 2020 at 06:23 PM 1
Share

Hi there I am a little confused at what you are trying to do .. if you are trying to store a collection of grid coordinates why not just store them in a list of Vector3/2s?

That said.

Could you rewrite your question explaining just the problem you are trying to solve as there is a lot of noise around it which will affect its "searchability" for people looking for similar answers in the future.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by logicandchaos · Feb 02, 2020 at 06:46 PM

In your ArraryList you are adding transforms to it so it will store transforms, if you want it to store positions you have to add that to it instead, familyArrayList.Add(grid[x, y]); should be familyArrayList.Add(grid[x, y].position); it looks like you are not just new to unity but to C# as well you need to learn the C# basics about variable types. If you use List instead of ArrayList you can strong type it, public List family; or public List family; depending on what you want. Also post your errors and code or people will not be able to help you, the error tells you what line the error occurred on and the type of error.

Comment
Add comment · Show 1 · 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 logicandchaos · Feb 03, 2020 at 04:38 PM 0
Share

if didn't print my in list declaration should be:

 public List<Transform> family; 

or

 public List<Vector3> family;

also if you want to convert a 1d array into a 2d array you have to divide by the number of columns.

avatar image
1

Answer by StefanJo · Feb 02, 2020 at 06:41 PM

ArrayList is untyped so in order to achieve what you are trying to the best option is to use List inside System.Collections.Generic:
List<Transform> familyList
Also you don't have to iterate over all the elements in the list in order to clear the list, you can just use familyList.Clear(); .

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

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

Related Questions

i auto generated a input manager c# script like Brackey's video and i am now getting compiler errors even though i did the exact same thing as him. Very confused. 2 Answers

Maintain local transform.rotation.y when updating transform.up? 2 Answers

3 Days and I still can't figure out c# reflection, please help? Trying to use a protected Type and method. 1 Answer

Player sinking into walls and floor with Raycasts 1 Answer

Android player crashes on startup 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