Search Unity

I figured out the way to build ingame with tilemaps, but I need a little help.

Discussion in '2D' started by aybarsmete1, Oct 27, 2019.

  1. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    So I recently discovered the command "SetTile", and I know it can be used to place tiles while ingame. But I have a little problem.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class ingameBuild : MonoBehaviour
    5. {
    6.     public Tile highlightTile;
    7.     public Tilemap highlightMap;
    8.  
    9.     private Vector3Int previous;
    10.     private void LateUpdate()
    11.     {
    12.         // current grid
    13.         Vector3Int currentCell = highlightMap.WorldToCell(transform.position);  
    14.  
    15.         if(Input.GetMouseButtonDown(0))
    16.         {
    17.             highlightMap.SetTile(currentCell, highlightTile);
    18.         }
    19.     }
    20. }
    21.  
    If you could help me whats wrong, I would really appreciate it.
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    You haven’t even written what’s wrong. You need to paste the error in, describe the behavior you want to occur and what actually happens, and so on. Making it as easy as possible for people to help you will give you a much higher chance of being helped.

    So, possible problems I can think of...
    - highlightTile is null
    - Tile is incorrectly defined
    - highlightTilemap is null
    - Position of game object with your script is in the wrong position.
    - You’ve configured the tilemap incorrectly.
    - Your script isn’t attached to a game object at all.
    - Your script or its game object isn’t enabled.
    - You intended to use that previous value and didn’t.
    - You forgot to write some code describing the actual behavior you want.
    - Some code elsewhere that I can’t see is broken.
     
  3. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    The problem is there is no such error code. It is the reason I am writing this thread, if there would have been one, I wouldn't even open this thread.
     
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Then what you do is you say what you're trying to make happen, and what's actually happening.

    Examples:
    - I am trying to place a tile, and when I click, no tile appears.
    - When I click, the tile appears in the wrong location.
    - The tile I placed doesn't look like how I think it should look.

    You get the idea.
     
  5. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    When I click, nothing appears.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    This will find the logical tile cell that overlaps the Transform position of the GameObject this script is on. Are you sure that's what you want? Are you sure that's what you want and not (for instance) the mouse position?

    Of course as clearly indicated above, you should actually present what the behaviour is and why it differs from what you expect. Posting code and assuming someone knows what you want isn't a good way to start. I know it sounds obvious but others have zero idea what you expect and have no knowledge of your project.
     
  7. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    I want to make a building system, instead of MathF.Round, I tried to use cell.
     
  8. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    UPDATE: I changed the code, however, it doesn't work as I expect. Also, I know the code is wrong for the situation I wanted to create. I believe you can help me.
    New Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class ingameBuild : MonoBehaviour
    5. {
    6.     public Tile highlightTile;
    7.     public Tilemap highlightMap;
    8.     [SerializeField]
    9.  
    10.  
    11.     // do late so that the player has a chance to move in update if necessary
    12.     private void LateUpdate()
    13.     {
    14.         // get current grid location
    15.         Vector3Int currentCell = highlightMap.WorldToCell(transform.position);    
    16.         // if the position has changed
    17.        
    18.             highlightMap.SetTile(currentCell, highlightTile);
    19.         }
    20.     }
    21.  
    22.  
    This is the video: https://cdn.discordapp.com/attachme...97206016/bandicam_2019-10-28_20-29-15-757.mp4
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    You've been asked to explain what it is you expect and what is happening. That video is confusing without context.
     
  10. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    I got it all solved, thanks actually. But it could be cool if you help me about making an object follow the pointer.