tiles not positioning (Fixed)

i am working on a game using a 2d chunk loading system i have been working on, and i tried to convert it to tiles, but the tiles remain at the position (0,0). Anyone know how to fix this?

code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class generation : MonoBehaviour
{
    [SerializeField] int width;
    [SerializeField] GameObject Player,Grass,Dirt,Stone,Coal,Wood;
    [SerializeField] Tile grass,dirt,stone,coal,wood;
    [SerializeField] Tilemap genchunk;
    [Range(0,500)]
    [SerializeField] float heightValue, smoothness;
    [SerializeField] float modifier,seed,treeModifier,coalModifier;
 
    // Start is called before the first frame update
    void Start()
    {
    seed = Random.Range(-1000000,1000000);
    chunkgen();
    Pgeneration((uint)0);
    }
public Grid grid;
public Tilemap[] worldchunks;
    // Update is called once per frame

     public void ClearChildren()
{
     int i = 0;

     //Array to hold all child obj
     GameObject[] allChildren = new GameObject[transform.childCount];

     //Find all child obj and store to that array
     foreach (Transform child in transform)
     {
         allChildren[i] = child.gameObject;
         i += 1;
     }

     //Now destroy them
     foreach (GameObject child in allChildren)
     {
        
         Transform e = child.gameObject.GetComponent<Transform>();
         int x1 = Mathf.RoundToInt(e.position.x);
         e = Player.gameObject.GetComponent<Transform>();
         int x2 = Mathf.RoundToInt(e.position.x);
        
         if(-Mathf.Abs(x1)+Mathf.Abs(x2) > 64)
         {
            
             for (i = 0; i < (x1/16+6); i++)
             {
                
                
                    if(worldchunks[(x1/16+7)] == null)
                    {
                    worldchunks[(x1/16)] = null;
                    Destroy(child.gameObject);
                  generateC((x1/16+7)*16); 
                    }
                if(worldchunks[(x1/16+1)] == null)
                {
                    worldchunks[(x1/16)] = null;
                    Destroy(child.gameObject);
                    generateC((x1/16+9)*16);
                }
                
                
             }
            
           
            
         }
        
     }


}

    void generateC(int x)
    {
        //worldchunks = new Tilemap[Mathf.RoundToInt(x/16)];
         Tilemap newChunk = Instantiate(genchunk, new Vector2(0,0), Quaternion.identity);
        newChunk.name = Mathf.Round(x/16).ToString();
        newChunk.transform.parent = this.transform;
        worldchunks[x/16] = newChunk;
        Transform cpos = newChunk.gameObject.GetComponent<Transform>();
        cpos.position = new Vector2(x,cpos.position.y);
        Pgeneration((uint)x);
    }

    void chunkgen()
    {
       
        int chunks = width/16;
       
        worldchunks = new Tilemap[1000000];

        for (int i = 0;i < chunks;i++)
        {
            Tilemap newChunk = Instantiate(genchunk, new Vector2(0,0), Quaternion.identity);
            newChunk.name = i.ToString();
            newChunk.transform.parent = this.transform;
            worldchunks[i] = newChunk;
            Transform cpos = newChunk.gameObject.GetComponent<Transform>();
            //cpos.position = new Vector2((i+1)*16,cpos.position.y);
           
        }
    }
    void Update()
    {
    ClearChildren();
    //Pgeneration();
        if(Input.GetMouseButtonDown(1))
        {
           
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector3Int position = grid.WorldToCell(mousePos);
        }
    }

 
    void Pgeneration(uint xoffset)
    {
int lastgen = 0;
int lastgencoal = 0;

    




        for (uint x = 0 + xoffset; x < width + xoffset; x++)
        {
        int height = Mathf.RoundToInt (heightValue * Mathf.PerlinNoise(x / smoothness, seed*63));
        int totalStoneSpawn = height - Mathf.RoundToInt(Mathf.PerlinNoise(x,(height*modifier)+seed));
            for (uint y = 0; y < height; y++)
            {
                if (Mathf.RoundToInt(y)<totalStoneSpawn-5)
                {
                    int minCoalChance = 0;
                    int maxCoalChance = 100;
                    int coalrange = Mathf.RoundToInt(x)+Mathf.RoundToInt(Mathf.PerlinNoise((lastgencoal*coalModifier)+seed,(Mathf.RoundToInt(y)*coalModifier)+seed));
                    int coalgen = Mathf.RoundToInt(x) - Mathf.RoundToInt(Mathf.PerlinNoise((x*coalModifier)+seed,(Mathf.RoundToInt(y)*coalModifier)+seed));
                    int coalValue = Random.Range(minCoalChance,maxCoalChance);
                    if (97 < coalValue)
                    {
                        if (Mathf.RoundToInt(y)<50000)
                        {
                            spawnOBJ(coal,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
                        }
                        else
                        {
                            spawnOBJ(stone,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
                        }
                       
                    }
                    else
                    {
                        spawnOBJ(stone,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
                    }
                   
                }
                else
                {
                  spawnOBJ(dirt,Mathf.RoundToInt(x),Mathf.RoundToInt(y));
                }
           
            }
           
            spawnOBJ(grass,Mathf.RoundToInt(x),height);
            float ypos = height;
            int range = Mathf.RoundToInt(x-xoffset) + Mathf.RoundToInt(Mathf.PerlinNoise((lastgen*modifier)+seed,ypos));
            int treegen = Mathf.RoundToInt(x-xoffset) - Mathf.RoundToInt(Mathf.PerlinNoise((Mathf.RoundToInt(x)*treeModifier)+seed,ypos));
           
            if (range>(lastgen+treegen)/1.99)
            {
                int tree = Mathf.RoundToInt(Mathf.PerlinNoise(Mathf.RoundToInt(x-xoffset),modifier*seed));
                    for (int add = 1;add<tree+4;add++)
                    {
                        spawnOBJ(wood,Mathf.RoundToInt(x),height+add);
                    }
                  
                    lastgen = lastgen+5;
              } 
              

           
           
               
           
        }
    }

    void spawnOBJ(Tile obj,int width,int height)
    {
        int chunkCoord = (Mathf.RoundToInt(width/16)* 16);
        chunkCoord /= 16;
       
        //obj = Instantiate(obj, new Vector2(width,height), Quaternion.identity);
        //obj.transform.parent = worldchunks[chunkCoord].transform;
        worldchunks[chunkCoord].SetTile(new Vector3Int(width,height,0),obj);
    }
}

Is line 108 the positioner and isn’t it commented out?

Either way, go to the line you THINK sets the position of the object, print what the calculations are, find out why they’re wrong.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Line 204 is the positioner, and I know the calculations are right because it was working before i decided to switch to tile maps to decrease lag.

And what happened when you did this:

i printed them to the debug log, and they were correct, but it still wasent spawning them right

EDIT: if you’re breaking this into a bunch of smaller tilemaps, what is moving the actual Tilemap to where it needs to be in the world?

That is certainly not line 204. That emplaces tiles. What moves the TileMap()?

line 108 positions the tile map, but in that code it is commented out

too much code, break it into smaller parts and see where it goes wrong

i already know it goes wrong somewhere at 204 because thats where it tries to position it

i have found out that it has something to do with the tilemaps cloning because if i tell it to place a tile on a regular tilemap, it works normal, but dosent load/unload chunks

I found the problem, turns out i didnt have a grid component, so it would not place them correctly