Search Unity

Cannot Find Method?

Discussion in 'Scripting' started by Marble, Aug 10, 2006.

  1. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    I'm using a slightly changed script I found hereabouts that fades the alpha of a texture and then destroys the script after it's done. I'm getting a runtime error "MissingMethodException: Cannot find method Destroy," however. Can anyone spot the mistake?

    Code (csharp):
    1. var initialDelay = 1.0;
    2. var fadeSpeed = 1.0;
    3.  
    4. private var originalColor;
    5. originalColor = renderer.material.color;
    6. renderer.material.color.a = 0.0;
    7.  
    8. yield new WaitForSeconds(initialDelay);
    9.  
    10. while (renderer.material.color.a < originalColor.a)
    11. {
    12. Debug.Log("Fading. Alpha = " + renderer.material.color.a);
    13.    renderer.material.color = Color.Lerp(renderer.material.color, originalColor, fadeSpeed * Time.deltaTime);
    14.    
    15.    yield;
    16. }
    17. Object.Destroy();
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Object.Destroy(); should be Destroy(gameObject);

    -Jon
     
  3. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Ah, but I don't want to destroy the gameObject the script is attached to, just the script Component.

    I'll be editing the alpha of the texture later and I don't want this script getting in the way.
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Then instead of passing the gameObject to Destroy, pass the component instead:

    Code (csharp):
    1.  
    2. Destroy(this);
    3.  
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
  6. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Thank you both.