Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to save the tiles position in 2048 game

Discussion in 'Scripting' started by GG_WP_EASY, Jul 13, 2018.

  1. GG_WP_EASY

    GG_WP_EASY

    Joined:
    Mar 30, 2018
    Posts:
    9
    Hey! I am working on a 2048 game and hit a problem with saving and loading tiles position. Everyone knows how the game field looks, there are rows and columns. So, every tile has row index (indRow) and column index (indCol), which set the tile position on the game field. How can I save this indexes in the game to load it in the next start? The error is: "NullReferenceException: Object reference not set to an instance of an object"
     

    Attached Files:

  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    657
    Couple of things:

    Please post your code in CODE tags rather than attaching a file. It's easier to read and reference.

    What line in your code is throwing the exception? The error you have just means you tried to access something that was null. It should be easy to tell from the error message which line has the problem, and put a breakpoint on that line to inspect what is null.

    I assume you have more code you haven't included, where you initialize the contents of AllTiles to actually contain some objects. You've allocated storage for the array on line 9 of the code you attached, but you haven't shown where you create new Tile objects.

    It's a bit odd that your Tile object is a MonoBehaviour. Unless you're adding some more functionality to it, you'd be better off with a struct instead.

    The way you're saying the values doesn't make sense. You're overwriting the row and column values with every pass through the loop. You'll need another way to explicitly reference the tiles. If you're going to keep using player prefs, you might consider an approach like this:

    Code (CSharp):
    1. for (var i = 0; i < 4; i++) {
    2.     for (var j = 0; j < 4; j++) {
    3.         var storageKey = string.format("Coordinates_R_{0}_{1}", i, j);
    4.         PlayerPrefs.SetInt(storageKey, AllTiles[i][j].indRow);
    5.     }
    6. }
    That would at least store distinct values for each tile. That said, you might consider going with another storage approach, like JSON serialization, if you're going to be storing more than the row and column.
     
  3. GG_WP_EASY

    GG_WP_EASY

    Joined:
    Mar 30, 2018
    Posts:
    9
    Thanks a lot, but it still doesn't work. I think, this proublem isn't solved without part of my code. Thanks for poining out my mistakes. So, I'll try to solve the proublem using your code example