Home > front end >  SceneManager does not contain a definition
SceneManager does not contain a definition

Time:10-27

Got a problem with using the scene manager. Already had a using UnityEngine.SceneManagement still shows the error of both LoadScene and GetActiveScene. I don't have a class such as "SceneManager". Here is the code.

using UnityEngine.SceneManagement;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    bool alive = true;

    public float speed = 5f;
    public Rigidbody rb;

    float horizontalInput;
    float horizontalMultiplier = 1.7f; 

    private void FixedUpdate()
    {
        if (!alive) return;


        Vector3 forwardMove = transform.forward * speed * Time.fixedDeltaTime;
        Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
        rb.MovePosition(rb.position   forwardMove   horizontalMove);
    }
    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        if (transform.position.y < -5)
        {
            end();
        }
    }

    public void end ()
    {
        alive = false;

        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

Here is the error I have.

enter image description here

CodePudding user response:

I believe you have to be using UnityEngine; before actually using UnityEngine.SceneManagement here's an example

using UnityEngine; using UnityEngine.SceneManagement;

the problem here is that your using a part or branch or something of EnityEngine before using UnityEngine itself

CodePudding user response:

First of all try:

UnityEngine.SceneManagement.SceneManager.LoadScene( "myScene");

There can be 2 troubles:

First -

This happened to me. I had downloaded an asset off the store called Falling Leaves. It included a script called SceneManager. Namespaces should be better utilized for store assets. I'll have to take a look at the code to my own to make sure I'm not breaking someone else's project unintentionally.

Second - if you are wirtting kinda your own lib for unity check that all .dll / projects was imported or if you are writting code in folder that markder with .asmdef this is tool in unity that helps with faster compile and more separeted development (there) and if you are do smth with this asmdef file and checkbox "include unity engine" is not setted as true this is too can be reason so check pls

Same question was answered there https://forum.unity.com/threads/scenemanager-does-not-contain-a-definition-for-loadscene.388892/

  • Related