Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tilemap.WorldToCell returns position of tilemap instead of given position when using a prefab

Discussion in '2D' started by leifgood, Oct 3, 2018.

  1. leifgood

    leifgood

    Joined:
    Nov 28, 2015
    Posts:
    12
    Is there a bug with Tilemap.WorldToCell?

    I have a grid GameObject with a tilemap child GameObject. I also have a tilemapManager script attached to another GameObject that has a field for the grid and acesses the tilemap with grid.transform.Find("tilemap").gameObject.GetComponent<Tilemap>(). TilemapManager has a method where tilemap.cellToWorld(position) is called.

    Here is what I cant get behind:

    When both of the mentioned GameObjects are in the scene, cellToWorld(position) returns the worldPosition of the given position.
    However, when I instead make grid a prefab and remove it from the scene, give the prefab to the tilemapManager and then call Instantiate(grid) in the tilemapManager before everything else, cellToWorld(position) returns the position of the tilemap, which in my case is (0,0,0).

    I dont understand what makes the difference here. Am i missing something with how prefabs in unity work?
     
  2. hamberge

    hamberge

    Joined:
    Aug 30, 2015
    Posts:
    36
    I think what you are doing is calling the celltoworld function on your prefab, not on the instantiated instance. This is because your line grid.transform.Find("tilemap").gameObject.GetComponent<Tilemap>() points to the prefab, which is an uninstantiated object. What you really want is to save the grid returned by Instantiate(grid) in some grid variable and then call the celltoworld function for that grid variable. In order to do that, you need separate grid variables, one for the prefab and one for the instance. The prefab would be something like:

    public Grid gridPrefab;


    The grid variable would be something like:

    private Grid grid;


    In Start, you would call:

    grid = Instantiate(gridPrefab);


    then you call:
    grid.transform.Find("tilemap").gameObject.GetComponent<Tilemap>()


    Does that make sense?
     
    Last edited: Oct 4, 2018
  3. ImperialDynamics

    ImperialDynamics

    Joined:
    Jun 21, 2018
    Posts:
    21
    i have a similar problem but in my case having two separate objects, the prefab and the instance, is not an option, here's why:
    I want to be able to find the cell under the mouse click.
    i'm using a custom brush editor.
    When the grid and its tilemap are in the scene i use WorldToCell(mousepos) and it works.
    When the grid and its tilemap are not in the scene but in the resources folder as a prefab the WorldToCell calls always returns (0,0,0)
    Is there any way to find the cell from the mouse click for the prefab?
    Instantiating it then doing it is not an option because when you are in prefab mode you have the ability to move freely and the same click position will correspond to different points in the instantiated tilemap and the prefab tilemap.
    Please help me :(