Баг в коде при создании нового объекта в unity
Случился такой баг, когда персонаж входит в триггер, должен создаваться объект, на случайной координате по y, но бывают такие случаи, когда объект просто не появляется, в коде это не отображается как ошибка. Вот сам код генерации объекта:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObstacles : MonoBehaviour
{
public GameObject Obstacle;
public float RandomY;
void Start()
{
}
void Update()
{
GameObject[] Obstacle;
Obstacle = GameObject.FindGameObjectsWithTag("Obstacle");
if(Obstacle.Length >=5)
{
Destroy(Obstacle[0]);
}
}
void Generator(Vector3 centre, float radius)
{
Collider[] colider = Physics.OverlapSphere(centre, radius);
if(colider.Length == 0)
{
Instantiate(Obstacle, centre, Quaternion.identity);
}
}
void OnTriggerEnter(Collider col)
{
RandomY = Random.Range(-4f, 4f);
if(col.tag == "Player")
{
Generator(new Vector3(transform.position.x - 18f,RandomY, - 12.01f), 1.0f);
}
}
}