Yes, that will not work. Like I said, that loop never returns control back to Unity so the frame never finishes.
Your boost and minimap icon is disconnected from each other. Why don’t you just let the boost remove the icon?
Let the minimap return the image, and then the boost can destroy it when the 15 seconds expire or the player picks it up
// changed to return the image
public Image GenerateMiniMapBoost(Vector3 boost_wm, int boostType, GameObject boost)
{
Image newImage = Instantiate(boost_mm[boostType]);
newImage.transform.SetParent(miniMap.transform,false);
//Finding the icon location in minimap. Works fine
newImage.rectTransform.anchoredPosition = ref1_mm.anchoredPosition +
new Vector2((boost_wm.x - ref1_wm.position.x)*miniMapRatio,
(boost_wm.y - ref1_wm.position.y)*miniMapRatio);
return newImage;
}
// Pickup script
GameObject boostObject;
Image boostImage;
// Call this when the boost is picked up by the player
void DestroyBoost()
{
Destroy(boostObject);
Destroy(boostImage);
}
void GenerateBoost()
{
int randomNumObject = Random.Range(0,boosts.Length);
int randomNumRoad = Random.Range(0,roads.Length);
Vector3 loc = roads[randomNumRoad].transform.position;
boostObject = Instantiate(boosts[randomNumObject], loc, Quaternion.identity) as GameObject;
boostImage = miniMap.GenerateMiniMapBoost(loc,randomNumObject,boost);
Invoke(nameof(DestroyBoost),15);
}
The other option would be to add an event on the boost and let the minimap listen for the event. You will have to be able to associate boosts to their respective images and when the boost is picked up or destroyed, you’d fire the event and the minimap can clean up the image