Hi,
I’m trying to clone an object several times and pass each clone to a different script.
But although the clones appear in the inspector, each script always links to the first clone. I don’t understand why this is happening, since the cones are created just fine.
Image shieldInd = shieldBar.transform.FindChild("ShieldIndicator").GetComponent<Image>();
int counter = 1;
for (int i = 0; i < ship.equipment.Count; i++ )
{
EquipmentSlot es = ship.equipment*;*
if (es.name == “Shield”)
{
Image newInd = null;
newInd = Instantiate(shieldInd, shieldBar.transform.position, Quaternion.identity) as Image;
newInd.transform.SetParent(shieldBar.transform);
//Align the indicator image along the bar
newInd.transform.position = new Vector2(shieldInd.transform.position.x, shieldInd.transform.position.y + (counter - 1) * (shieldInd.rectTransform.rect.height + 5));
//Activate the clone
newInd.gameObject.SetActive(true);
//Assign the clone to the corresponding shield
(es.item as Shield_Equipment).shieldIndicator = newInd;
counter++;
}
}
I think the relevant variables are self explanatory. If you have any questions, fell free to ask.
Why is newInd not updated to the instance of the latest clone?
Thank you for your help!