Search Unity

How to I add a die animation before removing the gameobject from list and destroy()?

Discussion in 'Scripting' started by adontzy, Oct 19, 2019.

  1. adontzy

    adontzy

    Joined:
    Oct 19, 2019
    Posts:
    2
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Collections;
    3. using UnityEngine;
    4. using System;
    5. using UnityEngine.UI;
    6.  
    7. public class Detection : MonoBehaviour
    8. {
    9.    
    10.     public Collider boxCollider;
    11.     public Collider detectionCollider;
    12.     public List<Transform> targetsList = new List<Transform>();
    13.     public Texture yourimage;
    14.     public float firerate = 1f;
    15.    
    16.  
    17.  
    18.      private void Start()
    19.     {
    20.         boxCollider = GetComponent<Collider>();
    21.      
    22.     }
    23.    
    24.     private void Update()
    25.     {
    26.         if ( Input.GetButton("Fire1"))
    27.         {
    28.  
    29.  
    30.             for (int i = targetsList.Count - 1; i > -1; i--)
    31.             {
    32.                 GameObject objToRemove = targetsList[i].gameObject;
    33.                 targetsList.RemoveAt(i);
    34.                 Destroy(objToRemove,firerate);
    35.             }
    36.  
    37.            
    38.         }
    39.     }
    40.  
    41.     private void OnTriggerEnter(Collider other)
    42.     {
    43.         if(other.CompareTag("Enemy"))
    44.         {
    45.             targetsList.Add(other.gameObject.transform);
    46.             Debug.Log("GameObejct enter" + 1*Time.deltaTime);
    47.            
    48.         }
    49.     }
    50.  
    51.     private void OnTriggerExit(Collider other)
    52.     {
    53.         if (other.CompareTag("Enemy"))
    54.         {
    55.             targetsList.Remove(other.gameObject.transform);
    56.             Debug.Log("GameObejct exit" + 1 *Time.deltaTime);
    57.            
    58.         }
    59.     }
    60.  
    61.     void OnGUI()
    62.     {
    63.         if (Input.GetButton("Fire1"))
    64.         {
    65.             for (int i = 0; i < targetsList.Count; i++)
    66.             {
    67.                 Vector3 screenPos = Camera.main.WorldToScreenPoint(targetsList[i].gameObject.transform.position);
    68.                 GUI.DrawTexture(new Rect(screenPos.x, screenPos.y, 60, 70), yourimage);
    69.                
    70.  
    71.             }
    72.         }
    73.     }
    74.  
    75.  
    76.  
    77.  
    78. }
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    What have you tried so far and it didn't work? Are you expecting people to add the missing code by just including everything you have so far. That is probably not going to happen.

    You could probably call a method in a target object that could start the animation, and then let the object self destruct, so that it handles it's own destruction.
     
  3. adontzy

    adontzy

    Joined:
    Oct 19, 2019
    Posts:
    2
    Ok sir I try write it my own way , appreciate ur reply,thks