Search Unity

Question How can I fix this?

Discussion in 'Scripting' started by LazyGhost15, May 20, 2023.

Thread Status:
Not open for further replies.
  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120
    I am using a grid in a 3D project so I changed the grid to XZY and the tilemap to XZ so the tiles would be horizontal instead of vertical and I wrote this script that should tell me the tile sprite:

    Code (CSharp):
    1. public class From2DTo3D : MonoBehaviour
    2. {
    3.     int rows = 100;
    4.     int cols = 100;
    5.     public Tilemap tilemap;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         Make3DMap();
    10.     }
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.      
    15.     }
    16.     void Make3DMap()
    17.     {
    18.         for(int x = -100; x < rows; x++)
    19.         {
    20.             for(int z = -100; z < cols; z++)
    21.             {
    22.                 if(tilemap.GetSprite(new Vector3Int(x, 0 , z)) != null)
    23.                 {
    24.                     Sprite sprite = tilemap.GetSprite(new Vector3Int(x , 0 , z));
    25.                     print(x+ " , " + z + " " + sprite.name);
    26.                 }
    27.                 else if(tilemap.GetSprite(new Vector3Int(x, 0, z)) == null)
    28.                 {
    29.                    // print(x + " , " + z + " null");
    30.                 }
    31.             }
    32.         }
    33.     }
    The problem is it only tells the sprites of tiles with 0 in the Z axis and I don't understand why....
     
  2. Draad

    Draad

    Joined:
    Feb 17, 2011
    Posts:
    325
    Hello, try to uncomment the line
    Code (CSharp):
    1. // print(x + " , " + z + " null");
    Maybe you only created tiles with z = 0 ? You should also show us of you do fill your tiles.

    Also, there is something strange in your code. You state that number of row and col is 100.
    But in you for loop you actually go from -100 to 100 (that is 200 tiles).

    Code (CSharp):
    1. for(int x = -100; x < rows; x++)
    2. {
    3.     for(int z = -100; z < cols; z++)
    4.     {
    5.      
    6.     }
    7. }
    8.  
    9. //It probably should be :
    10.  
    11. for(int x = 0; x < rows; x++)
    12. {
    13.     for(int z = 0; z < cols; z++)
    14.     {
    15.      
    16.     }
    17. }
     
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    120

    I tried the print null code you mentioned it just says that everything that isn't at z = 0 is null. I started at -100 because I wanted 200 tiles that start from -100, -100 to 100, 100.
    This is the grid as you can see it has tiles from -100 -100 to 100 100 (I left some null to check if they would be called null) I made sure that I filled all the tiles using the line.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
Thread Status:
Not open for further replies.