Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39497
In Process

askthedev.com Latest Questions

Asked: May 5, 20252025-05-05T20:14:24+05:30 2025-05-05T20:14:24+05:30

How can I revert a tile back to its original state after replacing it in a Unity tilemap at runtime?

anonymous user

I’m deep into developing this farm game in Unity, and I’ve hit a bit of a wall that I hope some of you might have insights on. The game implements a tilemap where each individual tile represents various crops and mud. The idea is that when the player interacts with a crop tile, it transforms that tile into a mud tile while simultaneously increasing the player’s inventory for that specific crop. That’s working fine!

However, my goal is to revert that tile back to its original crop state after a specific timer runs out. Right now, I’m not sure how to keep track of the original tile and change it back after the timer expires. I’ve thought about creating a custom tile class that derives from TileBase, but I’m stumped on the implementation details and how to manage the original state of tiles efficiently.

Currently, I have a script that handles the interaction where the player can replace a crop tile with a mud tile. My OnTrigger methods make sure that the player can only interact with the tile when they are in proximity, and the tile replacement happens when they press the spacebar. This all works nicely until the point where I need to revert the tile back to what it originally was.

I looked into a couple of places but wasn’t able to find a comprehensive guide or example on how to implement this custom tile approach or how to track and revert the state of tile tiles efficiently. I’m also not sure how to preserve the information about the original tile while using your custom tile class. Should I store additional data in the class, or is there a more straightforward approach to this problem?

If any of you have tackled a similar issue or have suggestions on how to approach this, I’d really appreciate it! I’m all ears for any recommendations on techniques or even existing resources or examples you might have come across. Thanks in advance!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-05-05T20:14:26+05:30Added an answer on May 5, 2025 at 8:14 pm

      One efficient approach is to use a dictionary or another data structure in your tile-handling script to temporarily store the original tile information, linking each tile position to its initial tile type. Whenever the player interacts with a tile, store its “original” state and start the timer. Utilize Unity’s Coroutine methods such as StartCoroutine() to handle delayed reversion neatly. Within this coroutine, wait for your specified delay using yield return new WaitForSeconds(), after which you replace the tile back to its stored original state.

      Alternatively, you can implement a custom tile class inheriting directly from TileBase, adding properties to hold original state data. However, the simpler solution might be using Unity’s built-in Tile class along with an external dictionary mapped on tile coordinates (Vector3Int) to manage and revert tiles efficiently. This avoids complexity in custom tile implementation and leverages straightforward Unity built-in functionality like Tilemap.SetTile(). Also, consider encapsulating your reverting logic clearly within dedicated functions, making tile-state management intuitive, scalable, and easy to debug.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-05-05T20:14:25+05:30Added an answer on May 5, 2025 at 8:14 pm

      It sounds like you’re working on an interesting project! Managing tile states can be tricky. I think your idea of creating a custom tile class that derives from TileBase is a good start. You can definitely maintain the original state of a tile in this custom class.

      Here’s a rough approach you could implement:

      1. Create a custom tile class that has a property to store the original tile type (like a crop or whatever). For example:
      2.         public class CustomTile : TileBase {
                    public TileBase originalTile;
                    // Other properties (like sprite, etc.)
                }
                
      3. When you replace a crop with a mud tile, save the current tile’s reference as the originalTile in your CustomTile class before you change it.
      4. Set a timer to wait for the specific duration after which you want to revert the tile. You can use a Coroutine for this. Something like:
      5.         StartCoroutine(RevertTileAfterDelay(tile, delay));
                
      6. In the coroutine, wait for the desired time using yield return new WaitForSeconds(delay) and then set the tile back to original by accessing originalTile.

      Here’s some pseudo-code to illustrate:

          IEnumerator RevertTileAfterDelay(Tile tile, float delay) {
              yield return new WaitForSeconds(delay);
              // Get the original tile from your custom tile class and reset
              tilemap.SetTile(tilePosition, originalTile);
          }
          

      As for tracking, make sure your tilemap knows about the original tile during the interaction. You can keep a dictionary or something similar that maps positions to their original tiles if needed.

      Hope that helps! Good luck, and keep experimenting! 🙂

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.