Destroy(this) don't destroy the script...

Hello Unity guy!
I learned a lot reading all your questions and answers, untill now I never had the need to write here… but the time is come. :smile:

I’m writing a script for fade in and out an NGUI panel. There’s a script UIPanelAlpha that is working great inside the NGUI package and I want to create an handle script to animate a fade in/out.
Following the script that maybe is useful for someone else:

public class FadePanel : MonoBehaviour
{
	public bool isFadeOut = false; 
	public float timeToFade = 1f;
	public float startAlpha = 0f;
	public float endAlpha = 1f;
	bool isFading = false;

  	void OnEnable(){
		StartCoroutine ( Fade( isFadeOut,  timeToFade,  startAlpha,  endAlpha  ));
	}
	
	public IEnumerator Fade( bool isFadeOut, float timeToFade, float startAlpha, float endAlpha)
	{
		float mStart = Time.realtimeSinceStartup;
		if (this.gameObject.GetComponent<UIPanel>() != null)
		{

			UIPanelAlpha panelAlpha =  this.GetComponent<UIPanelAlpha>();
			if (panelAlpha == null){
				panelAlpha =  this.gameObject.AddComponent<UIPanelAlpha>();
				panelAlpha.alpha = startAlpha;
			}
			isFading = true;
			float alpha = startAlpha;
			while (isFading){
				if (isFadeOut){
					alpha = (timeToFade > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 0f;
					if ((alpha < endAlpha)||(alpha < 0)) { 
						isFading = false;
					} 
				} 
				else {
					alpha = (timeToFade > 0f) ? 0f + Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 1f;	
					if ((alpha > endAlpha)||(alpha > 1)){ 
						isFading = false;
					}
				}	
				panelAlpha.alpha = alpha;	
				yield return null;
			}
			Destroy(panelAlpha);
		}
		Destroy(this);
	}
}

In my logic all is working… but something is wrong with the Destroy part.
Seems that the script don’t Destroy the panelAlpha nor itself.
Why?
I was crashing my head through the wall untill I thought that maybe someone of you could help me in this stupid thing…
Thanks!
Ivano

does isFading ever REALLY get set to false?

double check that those if checks are evaluating to true.

Hey joessu, thanks for the answer.
I double checked and you had reason.
isFading is not going to be false cause I wrong to check my calculated alpha instead of the one in UIPanelAlpha.
In the Update of UIPanelAlpha there’s alpha = Mathf.Clamp01(alpha); so due to the approximation differ from the one I have in my script and this was screwing my check.
So I fast fixed my script and now is working right, maybe is not the best implementation but for now is ok.

using UnityEngine;
using System.Collections;
[AddComponentMenu("NGUI/Interaction/FadePanel")]
public class FadePanel : MonoBehaviour
{
	public bool isFadeOut = false; 
	public float timeToFade = 1f;
	public float startAlpha = 0f;
	public float endAlpha = 1f;
	bool isFading = false;

  	void OnEnable(){
		StartCoroutine ( Fade( isFadeOut,  timeToFade,  startAlpha,  endAlpha  ));
	}
	
	public IEnumerator Fade( bool isFadeOut, float timeToFade, float startAlpha, float endAlpha)
	{
		float mStart = Time.realtimeSinceStartup;
		if (this.gameObject.GetComponent<UIPanel>() != null)
		{
			UIPanelAlpha panelAlpha =  this.GetComponent<UIPanelAlpha>();
			if (panelAlpha == null){
				panelAlpha =  this.gameObject.AddComponent<UIPanelAlpha>();
				panelAlpha.alpha = startAlpha;
			}
			isFading = true;
			float alpha = startAlpha;
			while (isFading){
				if (isFadeOut){
					alpha = (timeToFade > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 0f;
					if (( panelAlpha.alpha <= endAlpha)||(panelAlpha.alpha <= 0)) { 
						isFading = false;
					} 
				} 
				else {
					alpha = (timeToFade > 0f) ? 0f + Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 1f;	
					if ((panelAlpha.alpha >= endAlpha)||(panelAlpha.alpha >= 1)){ 
						isFading = false;
					}
				}	
				panelAlpha.alpha = alpha;	
				yield return null;
			}
			Destroy(panelAlpha);
		}
		Destroy(this);
	}
}

Ivano