• 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 Benjaben · Jan 19, 2011 at 06:39 PM · 2darrays

2D Arrays of GameObjects

I'm trying to make a 2D array of cubes for easy referencing, and I'm using this as my starting place. I tried testing it with integers and it works just fine, but whenever I try to use GameObjects I get a runtime error.

I added this to the MultiDim.cs:

public static GameObject[,] GameObjectArray (int a, int b) {
        return new GameObject[a,b];
    }

And then I have this in my main tester:

var cube : GameObject; var x = 2; var y = 2; var tiles;

function Start() { var i; var j;

 tiles = MultiDim.GameObjectArray(x,y);

 for(i = 0; i < y; i++)
     for(j = 0; j < x; j++)
     {
         Instantiate(cube, Vector3(j, 0, i), transform.rotation);
                 tiles[j,i] = cube;
     }

}

But I always get the runtime error: MissingFieldException: Field 'UnityEngine.GameObject[,].' not found;

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

3 Replies

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

Answer by Eric5h5 · Jan 19, 2011 at 06:48 PM

You can't use dynamic typing:

var tiles;

The point of using MultiDim is that it types the variable correctly using type inference:

var tiles = MultiDim.GameObjectArray(x,y);

You can resize the array later if necessary by using MultiDim again.

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 Benjaben · Jan 19, 2011 at 07:02 PM 0
Share

Thanks. $$anonymous$$y other problem is now that I want to name each of the objects in the array, but if I try tiles[0,0].name = "0,0"; it changes every object's name in the array to "0,0".

avatar image Benjaben · Jan 19, 2011 at 07:11 PM 0
Share

never$$anonymous$$d I got it. I didn't realize that Instantiate returns the instance.

avatar image
0

Answer by Benjaben · Jan 19, 2011 at 06:42 PM

Well I changed the line: tiles = MultiDim.GameObjectArray(x,y); to

var tiles = MultiDim.GameObjectArray(x,y);

and it worked? I guess I am not allowed to have global variables when using this?

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 Eric5h5 · Feb 16, 2011 at 12:38 AM

In Unity 3.2 you can just do

var tiles : GameObject[,];

and you don't need MultiDim anymore.

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 IMTRIGGERHAPPY9 · Sep 13, 2011 at 01:55 AM 0
Share

how do you put objects into this type of array though? like with the basic array you can use .add is there something similar with this type?

avatar image crazyKnight · Nov 10, 2011 at 09:46 AM 0
Share

how can me make a 2d array which can be edited in the inspector???

avatar image Eric5h5 · Nov 11, 2011 at 01:10 AM 0
Share

@crazy$$anonymous$$night: make a custom inspector.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Changing images using a shader on a 2D canvas? 0 Answers

How to Integrate a .JSON text array into the program? 1 Answer

[2D] Array Sorting / Sequential Arrays 1 Answer

2D Platformer (Unity 4.3) - How are the AudioClip arrays initialized? 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