Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Object doesn't delete... (Alpha Fade)

Discussion in '2D' started by rabirland, Nov 22, 2014.

  1. rabirland

    rabirland

    Joined:
    Nov 22, 2014
    Posts:
    29
    I have this script attached to several Objects:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FadeAway : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     bool IsRoot = true;
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.                 if (IsRoot)
    15.                         return;
    16.  
    17.         Color color = renderer.material.color;
    18.         color.a -= 0.1f;
    19.         renderer.material.color = color;
    20.  
    21.         if (renderer.material.color.a <= 0.1f)
    22.             DestroyObject(gameObject);
    23.         }
    24.  
    25.  
    26.     void Activate()
    27.     {
    28.         IsRoot = false;
    29.     }
    30. }
    31.  
    So when i compile and run the game, the objects are going to fade and stops excatly at 0.1f (Alpha)
    But the objects won't delete, but remain in the scene...
    Neither DestroyObject or Destroy worked...
     
  2. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
    It does work when you attach the script to a game object. If you make IsRoot public and toggle the value in the Inspector it should fade and be destroyed from the scene. Do you have a hierarchy of objects?

    public bool IsRoot = true;
     
  3. rabirland

    rabirland

    Joined:
    Nov 22, 2014
    Posts:
    29
    I already fixed the error...
    I changed the IsRoot by an Activate function...
    I sent a message to the object when it was created...
    I mistyped the sendmessage and the objects got a true isRoot value and never destroyed themselves...