• 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 olidadda · Apr 24, 2020 at 04:09 PM · arrays

populate 2d array with existing grid of gameobjects

Hi all, sorry for my ignorance but I would like to populate a 2d array on a spawner script with a grid of cubes already present in my scene. The cubes have an instance of a custom class "grid coordinates" on them which defines coordinates stored as x/y floats and which row/column they belong to (for later use).

I'm not sure what to do to get them into my array, I can create the array with the right number of indexes, but then when I get to the end of the two for loops I don't know how to reference my cubes and match them with the array coordinates.

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

Answer by Quickz · Apr 24, 2020 at 05:56 PM

From my understanding I'm assuming you want to make a 2D array containing your cubes. One simple way is making a serialized field that will have it's references specified in the editor. But since the editor can't serialize a 2D array, an alternative option is to make a custom class, put an array there and then make an array of those class instances.

Here's a quick example:

 using UnityEngine;
 
 public class TestScript : MonoBehaviour
 {
     public CollectionOfCubes[] cubes = new CollectionOfCubes[0];
 
     private void Start()
     {
         for (int i = 0; i < cubes.Length; i++)
         {
             for (int j = 0; j < cubes[i].instances.Length; j++)
             {
                 GameObject currentCube = cubes[i].instances[j];
                 Debug.Log($"Cube name: {currentCube.name}");
             }
         }
     }
 }
 
 [System.Serializable]
 public class CollectionOfCubes
 {
     public GameObject[] instances = new GameObject[0];
 }


If you want to write a better solution, maybe something that doesn't require dragging the cube references by hand, you can check out some of the following references:
https://docs.unity3d.com/ScriptReference/Transform.GetChild.html
https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

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
0

Answer by olidadda · Apr 24, 2020 at 07:53 PM

@Quickz Thank you so much, that's really helpful. I was thinking of assigning them explicitly to the 2D array as there are not too many of them, but I like your idea much better!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

128 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

Related Questions

Convert BuiltIn array to an Generic List? 1 Answer

snake problem 0 Answers

How to set parents of all objects in an array? 1 Answer

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

GameObject [,] variable will it be addresses? 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