Question by
Docherty · Aug 12, 2020 at 01:35 AM ·
c#instantiateprefabuser interface
How do I destroy a instantiated UI image that is a prefab but pushed the canvas?
Hello,
Was hoping someone can help me. I'm creating a game and i'm stuck at this part as this part and I assume I'm missing something. I'm creating a sorting game recycle, garbage, paper, and compost items. These random items appear in a sequence of 10. The code picks between 12 prefabs randomly and then drops them through the scene. I've created another code for the prefab to Instantiate a UI quip as seen here:
Now the problem is I can't destroy it. This image populates in the canvas and my head can't get around to accessing it and destroying. The code for Instantiating the ItemUI Image:` public class S_TomatoUI : MonoBehaviour {
public GameObject uiObject;
public float positionOffset;
protected Image uiImage;
// Start is called before the first frame update
void Start()
{
uiImage = Instantiate(uiObject, FindObjectOfType<Canvas>().transform).GetComponent<Image>();
uiImage.transform.SetAsFirstSibling();
}
// Update is called once per frame
void Update()
{
uiImage.transform.position = Camera.main.WorldToScreenPoint(transform.position + Vector3.up * positionOffset);
}
public void OnDestroy()
{
Destroy(uiImage.gameObject);
}
} ` Can someone please help me.
Thanks,
Comment