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. Dismiss Notice

Question Why is this happening?

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

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

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    80
    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.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         Make3DMap();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.        
    17.     }
    18.  
    19.  
    20.     void Make3DMap()
    21.     {
    22.         for(int x = -100; x < rows; x++)
    23.         {
    24.             for(int z = -100; z < cols; z++)
    25.             {
    26.                 if(tilemap.GetSprite(new Vector3Int(x, 0 , z)) != null)
    27.                 {
    28.                     Sprite sprite = tilemap.GetSprite(new Vector3Int(x , 0 , z));
    29.                     print(x+ " , " + z + " " + sprite.name);
    30.                 }
    31.                 else if(tilemap.GetSprite(new Vector3Int(x, 0, z)) == null)
    32.                 {
    33.                    // print(x + " , " + z + " null");
    34.                 }
    35.             }
    36.         }
    37.     }
    The problem is it only tells the sprites of tiles with 0 in the Z axis and I don't understand why....
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,192
    Er... don't you want to iterate y instead of z ??

    Then the Vector3Ints would look like:
    new Vector3Int( x, y, 0);


    At least that's the default setting. Perhaps you have a way of changing that?
     
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    80
    But because I changed the grid to horizontal instead of vertical shouldn't it be
    new Vector3Int(x, 0, z);
    ?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,306
Thread Status:
Not open for further replies.