- Home /
UI Object is there but can't destroy it.
I am implementing UnityAds in my game and have some UI I want to show and hide, for that I am Instantiating objects(using the InstantiateUI function) and then Destroying them.
All the UI I use is in a simple class:
[System.Serializable] public class ads{
public Text AdTxt;
public Button WatchAd;
public Button Skip;
public Text tyTxt;
[HideInInspector]
public GameObject _tyAd;
[HideInInspector]
public GameObject _AdTxt;
[HideInInspector]
public GameObject _SkipAd;
[HideInInspector]
public GameObject _WatchAd;
}
Note: the underscored GO are defined null and will later get assigned as the clones of the not underscored objects.
tyAd is called after the Ad is Shown,it deletes some UI I don't want anymore and creates a Text thanking the player for watching an ad.
public void tyAd(){
Destroy(Ads._AdTxt.gameObject);
Destroy(Ads._WatchAd.gameObject);
Destroy(Ads._SkipAd.gameObject);
Ads._tyAd = InstantiateUI(Ads.tyTxt.gameObject);
} Note: The Ads.tyTxt is a GO with a Text and a Button Component.
AdResume gets called after the player Clicks the Button:
public void AdResume(){
Time.timeScale=1;
print (Ads._tyAd.name);
Destroy(Ads._tyAd.gameObject);
}
Here is the problem: The Ads._tyAd.gameobject doesn't get destroyed and stays in the scene.
If you think this may have something to do with it, here is the InstantiateUI function:
GameObject InstantiateUI(GameObject UI){
GameObject clone=Instantiate(UI)as GameObject;
clone.transform.SetParent(GameObject.Find("Canvas").transform,false);
return clone;
}