Problems setting parent of instantiated objects

Can anybody tell me where i have gone wrong, I am trying to instantiate prefabs to a grid layout group component and for any given variable of slots i try to create i get a extra 50% of that given number that are unparented clones of clones, i.e if i want to create 10 slots it will add 10 slots to the inventory grid but it will spawn 5 unparented clones of clones ,20 will spawn 10 unparented unwanted clones

public class SlotControl : MonoBehaviour
{
public GameObject inventorySlotPrefab;
public Transform inventoryGrid;

public int MaxSlots;
[SerializeField]private int currentNumberOfSlots;

private void Start()
{
    //CountSlots();
    //if (currentNumberOfSlots != MaxSlots)
    //{
    //    SlotCreation();
    //}
}
private void Update()
{
    //if (MaxSlots != currentNumberOfSlots)
    //{
    //    SlotCreation();
    //}
    if (Input.GetKeyDown(KeyCode.Return))
    {
        if (MaxSlots != currentNumberOfSlots)
        {
            Debug.Log("Forced Update");
            SlotCreation();
        }
    }
}
public void CountSlots()
{
    {
        GameObject[] _inventorySlotPrefab = GameObject.FindGameObjectsWithTag("inventorySlotPrefab");
        Debug.Log("Found Slot Tags");
        currentNumberOfSlots = _inventorySlotPrefab.Length;
        Debug.Log("Slots Counted");
    }
}
public void SlotCreation()
{
    CountSlots();
    for (int i = 0; i < MaxSlots; i++)
    {
        //var obj = inventorySlotPrefab.transform;
        var obj = Instantiate(inventorySlotPrefab, Vector3.zero, Quaternion.identity, transform);
        obj.transform.SetParent(inventoryGrid.transform);
        if (currentNumberOfSlots < MaxSlots)
        {
            Debug.Log("Not Enough Slots");
            Instantiate(obj);
            CountSlots();
        }
        else if(currentNumberOfSlots > MaxSlots)
        {
            Debug.Log("Too Many Slots");
            Destroy(obj);
            CountSlots();
        }
        else if(currentNumberOfSlots == MaxSlots)
        {
            Debug.Log("Slot Count OK");
        }
        
    }
}

}

Problem is that you are creating new slots, while all slots has not appeared yet. You should check number of spawned slots after the for cycle end it`s work.

  public void SlotCreation()
    {
        List<GameObject> spawnedSlots = new List<GameObject>(); //All slots spawned in for cycle. 
        //If line above causes an error like "could not find type or namespace "List <>"", add "using System.Collections.Generic;" at the top of skript.

        for (int i = 0; i < MaxSlots; i++)
        {
            //var obj = inventorySlotPrefab.transform;
            spawnedSlots.Add(SpawnSlot());
            spawnedSlots[spawnedSlots.Count - 1].transform.SetParent(inventoryGrid.transform);
        }

        CountSlots(); //counting slots after spawning

        if (currentNumberOfSlots < MaxSlots)
        {
            int Difference = MaxSlots - currentNumberOfSlots;
            Debug.Log("Not Enough Slots");
            for (int i = 0; i < Difference; i++) //spawnes missing quantity of slots
            {
                spawnedSlots.Add(SpawnSlot());
            }
        }
        else if (currentNumberOfSlots > MaxSlots)
        {
            int Difference = currentNumberOfSlots - MaxSlots;
            Debug.Log("Too Many Slots");
            for (int i = 0; i < Difference; i++) //destroys excess amount of slots
            {
                int LastSpawnedSlotID = spawnedSlots.Count - 1; //ID of last spawned slot

                Destroy(spawnedSlots[LastSpawnedSlotID].gameObject);
                spawnedSlots.RemoveAt(LastSpawnedSlotID);
            }
        }
        else if (currentNumberOfSlots == MaxSlots)
        {
            Debug.Log("Slot Count OK");
        }

        GameObject SpawnSlot() //I took this out to prevent copying the code
        {
            return Instantiate(inventorySlotPrefab, Vector3.zero, Quaternion.identity, transform);
        }
    }

@unity_BUJQdslYm7EuFQ
thank you so much i’m kind of new to this i spent ages trying to figure the problem myself, there were a few ways i tried that almost worked but lesson learned i suppose.