• 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 awplays49 · Jun 25, 2016 at 06:13 PM · 2darraygenerationtile2d array

2D array generated with custom inspector, null when starting game?

Hey, I started making a simple game with unity just for fun, but I think I may have hit a roadblock. I have a generated 2D array of tile GameObjects, and using that variable works fine when in edit mode. However, when I start the game, that variable goes null. I know this is probably a "works-as-intended" feature, but how can I get around this?

Comment
Add comment · Show 3
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 Mindmapfreak · Jun 25, 2016 at 07:41 PM 0
Share

How exactly do you use your Array? How do you fill it in the Editor? What do you want to do with it?

avatar image awplays49 · Jun 26, 2016 at 09:36 PM 0
Share

@ChrisAngel$$anonymous$$indmapfreak hi, sorry for the delayed response, I mustve missed the email.

I have a 2D array that is given data when pressing a button made from a custom inspector. For some reason the data is only available during the function called by the button. After that, it becomes null :(

avatar image Cherno · Jun 26, 2016 at 09:44 PM 0
Share

I think I may have experienced the same problem before, however it was not specific to an array but rather to a custom class that was changed custom inspector.

I believe it has something to do with the way the Unity Editor serializes data. So, you could try two things: 1. $$anonymous$$ake the data not serialize (if you have the [Serializable] argument, remove it) 2. Use the [HideInInspector] to, well, hide variables.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 26, 2016 at 10:32 PM

2D arrays can't be serialized by Unity. You basically have two options:

  • use a 1d array of a custom class which contains another 1d array

  • use a "flattend" 1d array.

Example for the first case:

 //C#
 
 [System.Serializable]
 public class SubArray
 {
     public GameObject[] objs;
 }
 
 public class SomeScript : MonoBehaviour
 {
     public SubArray[] array;
 }

This is basically like a "jagged" array (a nested array) but it's serializable by Unity. You can access an element like this:

 GameObject o = array[dim0Index].objs[dim1Index];

The other solution is to use a flattend array. For this all you need is an additional integer variable that specifies the "width" of the 2d array:

 public int arrayWidth = 10;
 public GameObject[] array;

To access an element you just have to calculate the appropriate index manually. You can implement a get and set method for it:

 public GameObject GetElement(int xIndex, int yIndex)
 {
     int index = xIndex + yIndex * arrayWidth;
     return array[index];
 }
 
 public void GetElement(int xIndex, int yIndex, GameObject val)
 {
     int index = xIndex + yIndex * arrayWidth;
     array[index] = val;
 }

To create the array you just have to multiply your desired xSize with the ySize of the array:

 public void CreateArray(int xSize, int ySize)
 {
     array = new GameObject[xSize * ySize];
     arrayWidth = xSize;
 }

The 1D array can be serialized by Unity.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

75 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

Related Questions

2d Array persists during multiple game sessions 2 Answers

2D Array of GameObjects... 1 Answer

Issue with instantiating multiple clone GameObjects 1 Answer

2D Tile Map Question 0 Answers

2D array problem in C# 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges