Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

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

Issue with instantiating multiple clone GameObjects 1 Answer

2D Array of GameObjects... 1 Answer

2D Tile Map Question 0 Answers

How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 0 Answers


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