so im making this thing where it smoothly transitions a newly spawned object with lerp, but i cant figure out how to delete it whenever it is done
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class makeCustomer : MonoBehaviour
{
[SerializeField] public int maxCustomerCount = 10;
int Customers = 0;
float nat = 0.0f;
[SerializeField] public GameObject customer;
[SerializeField] public Vector3 customerSpawnPos;
[SerializeField] public float speed = 1000f;
[SerializeField] public List<GameObject> cusetomers = new List<GameObject>();
[SerializeField] public Vector3 customerDespawnPos;
Vector3 smoothMovement;
GameObject folder;
GameObject buyerObject;
private void Start()
{
folder = new GameObject("CustomerFolder");
}
private void Update()
{
if (Customers < maxCustomerCount && Time.time > nat)
{
nat += 0.5F;
buyerObject = Instantiate(customer, customerSpawnPos, Quaternion.identity);
buyerObject.transform.parent = folder.transform;
Customers++;
cusetomers.Add(buyerObject);
}
if (buyerObject != null)
{
if(Time.time>1f)
{
if (buyerObject.transform.position.x > 4.9F) //this is where i tried to remove it
{
Debug.Log("Hello world!");
Destroy(buyerObject);
Customers--;
}
}
}
}
private void FixedUpdate()
{
if(buyerObject != null)
{
foreach (GameObject buyerObject in cusetomers)
{
var startpos = buyerObject.transform.position;
buyerObject.transform.position = Vector3.Lerp(startpos, customerDespawnPos, Time.deltaTime * speed);
}
}
}
}