-1

I am trying to make an object slowly fade away and then get destroyed. I have been using iTween.fadeTo function to achieve the desired behavior. However, after the assigned time to finish the fadeTo() function, the object gets destroyed without fading away. I don't know what I need to set up to make the object fade away before get destroyed. (The code below is fixed to the desired behavior)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


void horizontalMovement ()
{
    mousePosition = Input.mousePosition;
    RaycastHit hit; 
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast (ray, out hit)) {
        if (hit.collider != null) {
            GameObject[] tiles = GameObject.FindGameObjectsWithTag("Floor");
            int interations = 0;
            foreach (GameObject tile in tiles) {
                interations = interations + 1;
                float tilePosition = tile.transform.localPosition.x; 
                float hitPostion = hit.transform.gameObject.transform.localPosition.x;
                float minimunValue = tilePosition - hitPostion;

                if (minimunValue == 0) {

                    iTween.FadeTo(tile, iTween.Hash(
                        "alpha", 1f,
                        "amount", 0f,
                        "time", 1f,
                        "onCompleteTarget", gameObject,
                        "onComplete", "destroy",
                        "oncompleteparams", tile)
                    );
                }
            }

            Debug.Log ("clean objects horizontally");
        }
    }
    Debug.Log ("Horizontal Movement is working" + mousePosition);
}

public void destroy(GameObject anyObject){
    Destroy (anyObject);
    Debug.Log ("Item will be destroyed");
}

1 Answers1

-1

Instead of Destroy() method you could use nameYourObject.SetActive(false); It will dissapear your object as well.

Hanako
  • 1,637
  • 1
  • 13
  • 16