Search Unity

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

Discussion in 'Scripting' started by zackivano, Dec 21, 2012.

  1. zackivano

    zackivano

    Joined:
    Mar 20, 2012
    Posts:
    11
    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. :D

    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:

    Code (csharp):
    1. public class FadePanel : MonoBehaviour
    2. {
    3.     public bool isFadeOut = false;
    4.     public float timeToFade = 1f;
    5.     public float startAlpha = 0f;
    6.     public float endAlpha = 1f;
    7.     bool isFading = false;
    8.  
    9.     void OnEnable(){
    10.         StartCoroutine ( Fade( isFadeOut,  timeToFade,  startAlpha,  endAlpha  ));
    11.     }
    12.    
    13.     public IEnumerator Fade( bool isFadeOut, float timeToFade, float startAlpha, float endAlpha)
    14.     {
    15.         float mStart = Time.realtimeSinceStartup;
    16.         if (this.gameObject.GetComponent<UIPanel>() != null)
    17.         {
    18.  
    19.             UIPanelAlpha panelAlpha =  this.GetComponent<UIPanelAlpha>();
    20.             if (panelAlpha == null){
    21.                 panelAlpha =  this.gameObject.AddComponent<UIPanelAlpha>();
    22.                 panelAlpha.alpha = startAlpha;
    23.             }
    24.             isFading = true;
    25.             float alpha = startAlpha;
    26.             while (isFading){
    27.                 if (isFadeOut){
    28.                     alpha = (timeToFade > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 0f;
    29.                     if ((alpha < endAlpha)||(alpha < 0)) {
    30.                         isFading = false;
    31.                     }
    32.                 }
    33.                 else {
    34.                     alpha = (timeToFade > 0f) ? 0f + Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 1f;
    35.                     if ((alpha > endAlpha)||(alpha > 1)){
    36.                         isFading = false;
    37.                     }
    38.                 }  
    39.                 panelAlpha.alpha = alpha;  
    40.                 yield return null;
    41.             }
    42.             Destroy(panelAlpha);
    43.         }
    44.         Destroy(this);
    45.     }
    46. }
    47.  
    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
     
    Last edited: Dec 21, 2012
  2. joessu

    joessu

    Joined:
    Nov 16, 2010
    Posts:
    88
    does isFading ever REALLY get set to false?

    double check that those if checks are evaluating to true.
     
  3. zackivano

    zackivano

    Joined:
    Mar 20, 2012
    Posts:
    11
    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.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [AddComponentMenu("NGUI/Interaction/FadePanel")]
    4. public class FadePanel : MonoBehaviour
    5. {
    6.     public bool isFadeOut = false;
    7.     public float timeToFade = 1f;
    8.     public float startAlpha = 0f;
    9.     public float endAlpha = 1f;
    10.     bool isFading = false;
    11.  
    12.     void OnEnable(){
    13.         StartCoroutine ( Fade( isFadeOut,  timeToFade,  startAlpha,  endAlpha  ));
    14.     }
    15.    
    16.     public IEnumerator Fade( bool isFadeOut, float timeToFade, float startAlpha, float endAlpha)
    17.     {
    18.         float mStart = Time.realtimeSinceStartup;
    19.         if (this.gameObject.GetComponent<UIPanel>() != null)
    20.         {
    21.             UIPanelAlpha panelAlpha =  this.GetComponent<UIPanelAlpha>();
    22.             if (panelAlpha == null){
    23.                 panelAlpha =  this.gameObject.AddComponent<UIPanelAlpha>();
    24.                 panelAlpha.alpha = startAlpha;
    25.             }
    26.             isFading = true;
    27.             float alpha = startAlpha;
    28.             while (isFading){
    29.                 if (isFadeOut){
    30.                     alpha = (timeToFade > 0f) ? 1f - Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 0f;
    31.                     if (( panelAlpha.alpha <= endAlpha)||(panelAlpha.alpha <= 0)) {
    32.                         isFading = false;
    33.                     }
    34.                 }
    35.                 else {
    36.                     alpha = (timeToFade > 0f) ? 0f + Mathf.Clamp01((Time.realtimeSinceStartup - mStart) / timeToFade) : 1f;
    37.                     if ((panelAlpha.alpha >= endAlpha)||(panelAlpha.alpha >= 1)){
    38.                         isFading = false;
    39.                     }
    40.                 }  
    41.                 panelAlpha.alpha = alpha;  
    42.                 yield return null;
    43.             }
    44.             Destroy(panelAlpha);
    45.         }
    46.         Destroy(this);
    47.     }
    48. }
    Ivano