- Home /
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");
}
}
}
}
Answer by unity_BUJQdslYm7EuFQ · Jul 17, 2021 at 08:27 AM
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);
}
}
Answer by Lukey695 · Jul 17, 2021 at 09:44 PM
@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.
Ok ran into a few issues after swapping MaxSlot And Current Slot i fixed an out of range argument else if (currentNumberOfSlots > MaxSlots) int Difference = MaxSlots - currentNumberOfSlots ; //these 2 but the other issue I have been having is that when the MaxSlots variable changes during runtime to a number lower than the CurrentNumberOfSlots the script spawns a number of slots equal to the max slots value repeatedly on call and doesnt destroy the excess slots prefabs
Your answer
Follow this Question
Related Questions
How do I update player tilt in multiplayer? 0 Answers
How to Parent a Cloned Object to Another Cloned Object 1 Answer
Layout Group not working when instantiate from prefab 1 Answer
Error : Transform.parent not working properly. 1 Answer
Creating text at the position of the gameobjects in world 0 Answers