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 /
  • Help Room /
avatar image
0
Question by HectorSmoke · Jul 22, 2016 at 11:03 PM · editorarraylisttilemapplaymode

Preventing an array from clearing when playing game.

I am currently in the early stages of working on a 2D turn-based strategy game (think Fire Emblem). I'm creating my own tile map editor to be used in the Unity editor, here is the code for the map so far:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 
 [System.Serializable]
 public class Map : MonoBehaviour {
 
     public Vector2 mapDimensions = new Vector2(5, 5); //The width and height of the map in tiles.
     public Vector2 tileSize = new Vector2 (32, 32); //The width and height of a single tile in pixels.
     public Color gridColour = Color.green;
     public Color SelectionColour = Color.red;
 
     private GameObject tileParent;
     private GameObject unitParent;
     private Tile mouseOverTile;
 
     [SerializeField]
     private Tile[] tiles = new Tile[0];
 
     private Vector2 _currentGridDimensions;
     public Vector2 currentGridDimensions{
         get{ return _currentGridDimensions; }
     }
 
         
     void OnEnable(){
         InputManager.MousePositonBroadcast += CheckMouseOverTile;
     }
 
     void OnDisable(){
         InputManager.MousePositonBroadcast -= CheckMouseOverTile;
     }
 
     public void CreateTileParent(){
         if (tileParent != null) {
             return;
         }
 
         tileParent = new GameObject ();
         tileParent.name = "Tiles";
         tileParent.transform.SetParent (transform);
     }
 
     public void ConfigureUnitParent (Unit unit){
         if (unitParent == null) {
             unitParent = new GameObject ();
             unitParent.name = "Units";
             unitParent.transform.SetParent (transform);
         }
 
         if(!unit.transform.IsChildOf(transform)){
             unit.transform.SetParent (unitParent.transform);
         }
     }
 
     public void Retile(int xSize, int ySize, GameObject tilePrefab){
 
         var newTileArray = new Tile[xSize * ySize];
 
         int smallestX = xSize < _currentGridDimensions.x ? xSize : (int) _currentGridDimensions.x;
         int smallestY = ySize < _currentGridDimensions.y ? ySize : (int) _currentGridDimensions.y;
 
         var newI = 0; //Used to index the new array in the following loop.
         var currentI = 0; //Used to index tiles array in the following loop.
         var x = 0;
 
         while (newI < smallestY * xSize) {
 
             newTileArray [newI] = tiles [currentI];
             newI++;
             currentI++;
             x++;
 
             if(x >= smallestX){
                 if (_currentGridDimensions.x < xSize) {
                     while (x < xSize) {
                         AddTileToGrid (newTileArray, newI, xSize, tilePrefab);
                         newI++;
                         x++;
                     }
                     x = 0;
                 } else {
                     while (x < _currentGridDimensions.x) {
                         DestroyImmediate (tiles[currentI].gameObject);
                         currentI++;
                         x++;
                     }
                     x = 0;
                 }
             }
         }
 
         if (tiles.Length < newTileArray.Length) {
             for (int i = newI; i < newTileArray.Length; i++) {
                 AddTileToGrid (newTileArray, i, xSize, tilePrefab);
             }
         } else {
             for (int i = currentI; i < tiles.Length; i++) {
                 DestroyImmediate (tiles [i].gameObject);
             }
         }
 
         _currentGridDimensions = new Vector2 (xSize, ySize);
         tiles = newTileArray;
     }
 
     private void AddTileToGrid(Tile[] grid, int i, int xSize, GameObject tilePrefab){
 
         var x = i % xSize;
         var y = i / xSize;
 
         var newTile = Instantiate (tilePrefab);
         newTile.name = "Tile ("+x +", "+y+")";
         newTile.transform.SetParent (tileParent.transform);
         newTile.transform.position = new Vector3 (transform.position.x + x * tileSize.x, 
             transform.position.y + y * tileSize.y, 
             newTile.transform.position.z);
 
         var tile = newTile.GetComponent<Tile> ();
         tile.mapPosition = new Vector2 (x, y);
             
         grid [i] = newTile.GetComponent<Tile>();
 
     }
         
     public Tile GetTile(int x, int y){
 
         Debug.Log ("Retrieving Tile");
 
         if (tiles == null || tiles.Length == 0)  {
             return null;
         }
 
         return tiles [(int)(y * _currentGridDimensions.y + x)];
     }
 
     // Subscribed to EventManager's MousePositonBroadcast event. Checks to see which of it's tiles the mouse is over, if any.
     private void CheckMouseOverTile(Vector3 positon){
 
         var x = (int)((positon.x - transform.position.x + tileSize.x / 2) / tileSize.x);
         var y = (int)((positon.y - transform.position.y + tileSize.y / 2) / tileSize.y);
 
         Debug.Log (tiles.Length);
 
         Tile newMouseOverTile = null;
         if (x < mapDimensions.x && x >= 0 && y < mapDimensions.y && y >= 0) {
             newMouseOverTile = tiles [(int)(y * _currentGridDimensions.y + x)].GetComponent<Tile>();
         }
 
         if (newMouseOverTile != mouseOverTile) {
             mouseOverTile.gameObject.GetComponent<SpriteRenderer> ().color = Color.white;
             mouseOverTile = newMouseOverTile;
         }
         else if(newMouseOverTile != null){
             mouseOverTile.gameObject.GetComponent<SpriteRenderer> ().color = Color.blue;
         }
 
     }
 
     //Draws the grid for the map.
     void OnDrawGizmos(){
 
         Gizmos.color = gridColour;
         var cellSize = new Vector3 (tileSize.x, tileSize.y, 1);
 
         var startX = transform.position.x;
         var startY = transform.position.y;
 
         for (int x = 0; x < mapDimensions.x; x++) {
             for (int y = 0; y < mapDimensions.y; y++) {
 
                 var xPos = startX + tileSize.x * x;
                 var yPos = startY + tileSize.y * y;
 
                 var cellPosition = new Vector3 (xPos, yPos, 1);
                 Gizmos.DrawWireCube (cellPosition, cellSize);
 
             }
         }
 
     }
 
 }
 

Within the map object I want to store an array of tiles, that will eventually be connected to one another to create a graph for path-finding. The plan is to make that array of tiles (called 'tiles' in the above code) changeable in the editor to allow for easy map editing when we come to creating levels, but Unity clears this array when I go to play the game itself, is there any way around this? I have researched and tried multiple approaches, for example - the tiles were originally held in a list, then a two-dimensional array, and now just a one-dimensional array, and I've tried things like 'Serializable' - but I keep encountering the same problem. Can anyone think of a way I can bring that array of tiles from the unity editor, into the game itself without losing the tiles? Ideally I would like to store the tiles in a list or 2d array like I had I originally planned.

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 Firedan1176 · Jul 29, 2016 at 11:54 PM 0
Share

Try removing [System.Serializable] above your class. You only need to serialize classes that don't derive from $$anonymous$$onoBehaviour, and if you want them to appear in the inspector. An example of that:

 [System.Serializable]
 public class Egg {
     public float size = 1;
 }
 
 public class Script : $$anonymous$$onoBehaviour {
     //In the inspector, you would/should see 'size' show up
     public Egg myEgg;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheBlackBurrito · Jul 30, 2016 at 05:38 AM

You also need to add the [System.Serializable] to your Tile class, even if you have the tag [SerializeField] on the list it won't have an effect if your list type isn't Serializable.

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 HectorSmoke · Aug 05, 2016 at 10:23 PM

@TheBlackBurrito, I tried that, and it's certainly closer to what I want, as the tiles themselves aren't cleared from the scene, but the instances of the Tile are still cleared from the array. Is there any thing else I'm misssing? Here is the tile class so far:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class Tile : MonoBehaviour{
 
     public List<Tile> adjacentTiles;
     public Vector2 mapPosition;
     public Unit unit;
 
     public void ConnectTo(Tile tile){
         if (adjacentTiles.Contains (tile)) {
             return;
         }
 
         adjacentTiles.Add (tile);
         tile.adjacentTiles.Add (this);
     }
 
     public void DisconnectFrom(Tile tile){
         if (!adjacentTiles.Contains (tile)) {
             return;
         }
 
         adjacentTiles.Remove (tile);
         tile.adjacentTiles.Remove (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

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

77 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

Related Questions

Cannot implicitly convert type `System.Collections.Generic.List' to `UnityEngine.Vector3[]' 1 Answer

[Quiz Game] How to prevent Question asked twice. HELP 1 Answer

HTC Vive very laggy in editor. Laggy hands and stuttering. Profiler shows a lot of lag spikes from editor. 1 Answer

Code executing in play mode when it shouldn't 0 Answers

Keeping track of dynamic positions of items in a grid using List/Array? 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