Проблема в генерации уровня

Рейтинг: 0Ответов: 0Опубликовано: 15.07.2023

Я начинающий разработчик в создании игр. создал генерацию (Сделал с ютуба:)) и почему-то выдает вот такую ошибку: NullReferenceException: Object reference not set to an instance of an object RoomSpawner.Spawn () (at Assets/Scenes/Scripts/RoomSpawner.cs:40). знаю про что ошибка но уже несколько дней не могу исправить. Я много что пробовал но вы тоге не получилось исправить проблему. Вот весь скрипт:

public class RoomSpawner : MonoBehaviour
{
    public Direction direction;

    public enum Direction
    {
        Top,
        Bottom,
        Left,
        Right,
        None
    }

    private RoomVariants variants;
    private int rand;
    private bool spawned = false;
    private float waitTime = 3f;

    private void Start()
    {
        variants = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomVariants>();
        Destroy(gameObject, waitTime);
        Invoke("Spawn", 0.2f);
    }
    public void Spawn()
    {
        if (!spawned)
        {
            if (direction == Direction.Top)
            {
                rand = Random.Range(0, variants.topRooms.Length);
                Instantiate(variants.topRooms[rand], transform.position, variants.topRooms[rand].transform.rotation);
            }
            else if (direction == Direction.Bottom)
            {
                rand = Random.Range(0, variants.bottomRooms.Length);
                Instantiate(variants.bottomRooms[rand], transform.position, variants.bottomRooms[rand].transform.rotation);
            }
            else if (direction == Direction.Right)
            {
                rand = Random.Range(0, variants.rightRooms.Length);
                Instantiate(variants.rightRooms[rand], transform.position, variants.rightRooms[rand].transform.rotation);
            }
            else if (direction == Direction.Left)
            {
                rand = Random.Range(0, variants.leftRooms.Length);
                Instantiate(variants.leftRooms[rand], transform.position, variants.leftRooms[rand].transform.rotation);
            }
            spawned = true;
        }
    }
    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag ("RoomPoint") && other.GetComponent<RoomSpawner>().spawned)
        {
            Destroy(gameObject);
        }
    }
}

Вроде все правильно тэг связан со скриптом и просится найти объект и да мне кажется ошибка где-то в этой строчке кода:

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag ("RoomPoint") && other.GetComponent<RoomSpawner>().spawned)
        {
            Destroy(gameObject);
        }
    }

кто знает где ошибка пожалуйста скажите. начинающий создатель игр на юнити Все спасибо за внимание

Ответы

Ответов пока нет.