Why wont my script instantiate multiple instances?
I'm trying to make a script that instantiates a slot prefab into a UI Panel multiple times. After instantiated, the slots need to be given a parent. This is a snippet of my code:
function createInventory () {
for (var i: int = 0; i < 10; i++) {
var Clone = Instantiate(slotObject, inventoryParent.transform.position, inventoryParent.transform.rotation);
Clone.transform.parent = inventoryParent.transform;
}
}
It only seems to be instantiating one slot at the moment. What can I do to fix this? Thanks in advance!
Answer by Godefroy_dc · Nov 23, 2015 at 07:33 PM
Hi,
First I think you don't need to set the same position and rotation as inventoryParent to Clone because Clone is a child of inventoryParent.
for (var i: int = 0; i < 10; i++) {
var Clone = Instantiate(slotObject);
Clone.transform.SetParent(inventoryParent.transform, false);
}
Have you added a Layout Group to inventoryParent ? If no, maybe your Clones are all at the same position and rotation. Try a VerticalLayoutGroup for example ;-) And if your Clones size shrinks to 0, add a Layout Element to your prefab to define a min and max size.
Thank you for your answer. It seemed to be an issue with having to reload the prefab into the script again from unity, however, it is all sorted now. Thanks
Your answer
Follow this Question
Related Questions
Instantiate Script Not Working 1 Answer
Instantiate an object as child 1 Answer
Why all instantiated objects get triggered when 1 is? 0 Answers
Problem with instantiate and destroy multiple objects 0 Answers
Create only one Instantiated Object 1 Answer