Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Destroy Object from prefabList

Discussion in 'Scripting' started by unity_55FA1B9DF2C26C2E7399, Aug 19, 2021.

  1. unity_55FA1B9DF2C26C2E7399

    unity_55FA1B9DF2C26C2E7399

    Joined:
    May 26, 2021
    Posts:
    2
    Hey folks,

    I need your help regarding the Destroy method.
    I want to destroy a car, when it has driven by and it cannot be seen in the camera anymore.
    I used a Trigger to destroy the "Tile" when it passed by but as the cars are in a prefabList, I cannot select a specific object to destroy.
    I made a video to demonstrate my problem and attached my code for the car spawning below.

    Thank you so much for your help!

    Kind regards
    Tim

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CarSpawn : MonoBehaviour
    6. {
    7.     List<GameObject> prefabList = new List<GameObject>();
    8.     public GameObject carSpawnGreen;
    9.     public GameObject carSpawnBlue;
    10.     public GameObject carSpawnPolice;
    11.     public GameObject carSpawnBus;
    12.     public GameObject carSpawnVan;
    13.     public GameObject carSpawn1;
    14.     public GameObject carSpawn2;
    15.     public GameObject carSpawn3;
    16.     public GameObject carSpawn4;
    17.     public GameObject carSpawn5;
    18.     public GameObject carSpawn6;
    19.     public GameObject carSpawn7;
    20.     public GameObject carSpawn8;
    21.     public float speed = 30F;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         StartCoroutine(Waiter());
    26.         prefabList.Add(carSpawnBlue);
    27.         prefabList.Add(carSpawnGreen);
    28.         prefabList.Add(carSpawnPolice);
    29.         prefabList.Add(carSpawnBus);
    30.         prefabList.Add(carSpawnVan);
    31.         prefabList.Add(carSpawn1);
    32.         prefabList.Add(carSpawn2);
    33.         prefabList.Add(carSpawn3);
    34.         prefabList.Add(carSpawn4);
    35.         prefabList.Add(carSpawn5);
    36.         prefabList.Add(carSpawn6);
    37.         prefabList.Add(carSpawn7);
    38.         prefabList.Add(carSpawn8);
    39.     }
    40.  
    41.     IEnumerator Waiter()
    42.     {
    43.         int prefabIndex = Random.Range(0, 8);
    44.         int wait_time = Random.Range(1, 5);
    45.         yield return new WaitForSeconds(wait_time);
    46.         int carPicker = Random.Range(1, 3);
    47.         if (carPicker == 1)
    48.         {
    49.             Instantiate(prefabList[prefabIndex], new Vector3(44.49F, 0.85F, 100F), Quaternion.Euler(new Vector3(0F, 90, 0F)), transform);
    50.         }
    51.         else
    52.         {
    53.             Instantiate(prefabList[prefabIndex], new Vector3(47.21F, 0.85F, 100F), Quaternion.Euler(new Vector3(0F, 90, 0F)), transform);
    54.         }
    55.  
    56.         StartCoroutine(Waiter());
    57.     }
    58.  
    59.     // Update is called once per frame
    60.     void Update()
    61.     {
    62.         transform.Translate(Vector3.back * speed * Time.deltaTime);
    63.     }
    64.  
    65.     private void OnTriggerEnter(Collider other)
    66.     {
    67.         //Destroy cars when touching the trigger
    68.     }
    69. }
     
  2. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Silly question, why destroy a car rather than store it in an object pool?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    You have a fundamental flaw above: your spawner is trying to figure out who died when they hit the trigger.

    Bluntly that is none of the business of the spawner. That's the business of the car to decide.

    Therefore, instead put a script to destroy the car ON THE CAR, and have that respond to the trigger and destroy itself.

    Go look at pretty much any tutorial on triggers or colliders to see this in action.