0

У меня есть следующий код, для управления персонажем. Но при запуске программы выходит ошибка SceneManager does not contain a definition for 'GetActiveScene. Как я понял данная ошибка говорит о том, что у SceneManager нет метода GetActiveScene. Но я не однократно встречал данный код для реализации перезагрузки сцены.

В интернете я не нашёл решение данной проблемы.

Версия Unity - 2020.3.11f1

Весь код:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{

    [SerializeField] private Transform feetPos;
    [SerializeField] private LayerMask whatIsGround;

    private Rigidbody2D _rigidbody;
    private Animator _anim;

    private float checkRadius = 0.3f;
    private float normalSpeed = 3f;
    private float speed = 0;
    private float jumpForce = 7f;

    private bool facingRight = true;
    private bool isGround = true;

    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
        _anim = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        isGround = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
        _rigidbody.velocity = new Vector2(speed, _rigidbody.velocity.y);

        if (transform.position.y < 0)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }

    public void RunLeft()
    {
        _anim.SetBool("isWalk", true);
        if (facingRight)
        {
            transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
            facingRight = false;
        }
        if (speed >= 0f)
            speed = -normalSpeed;
    }

    public void RunRight()
    {
        _anim.SetBool("isWalk", true);
        if (!facingRight)
        {
            transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
            facingRight = true;
        }
        if (speed <= 0f)
            speed = normalSpeed;
    }

    public void OnButtonUp()
    {
        speed = 0f;
        _anim.SetBool("isWalk", false);
    }

    public void Jump()
    {
        if (isGround)
        {
            _anim.SetTrigger("Jump");
            _rigidbody.velocity = Vector2.up * jumpForce;
        }
    }
}

Часть кода в которой ошибка:

if (transform.position.y < 0)
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}

1 ответ 1

1

Уверен, что где-то в коде у вас есть собственный скрипт, который называется SceneManager с одноимённым классом. Вот он-то и мешается, потому что идёт конфликт имён

1
  • Спасибо большое! Вы оказались абсолютно правы.
    – Veter_ok
    4 июн 2021 в 5:45

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.