0

I'm making a 2D platformer in Unity3D (you can only control along the X axis, and Y axis by jumping) and I'm trying to move to the next stage by loading the next scene.

I've tried looking all over stack overflow and other sites and I can't find anything to help. Here's my code:

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

public class SceneManager : MonoBehaviour
{
    public void LoadScene(string level)
    {
        SceneManager.LoadScene(level);
    }
}

public class Player : MonoBehaviour
{
    public Rigidbody rb;

    public float jumpForce = 10f;
    public float sidewaysForce = 10f;

    bool isGrounded = true;
    string myObjects = "";


    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.GetKey("d"))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (Input.GetKey("a"))
        {
            rb.AddForce((0 - sidewaysForce) * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if ((Input.GetKeyDown("space")) && isGrounded)
        {
            rb.AddForce(0, jumpForce * Time.deltaTime, 0, ForceMode.VelocityChange);
        }


    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }

        if (collision.gameObject.CompareTag("Death"))
        {
            transform.position = new Vector3(0, 4, 0);//(where you want to teleport)
        }

        if (collision.gameObject.CompareTag("NextT"))
        {
            SceneManager myObject = new SceneManager();
            myObject.LoadScene("Level1");
        }
    }

    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

The message is;

Assets\Player.cs(10,9): error CS0120: An object reference is required for the non-static field, method, or property 'SceneManager.LoadScene(string)'

and as it is a compiler error I can't start the game. By the way, I'm really new to C# so please overlook any inefficiencies in the code. :)

mreyeball
  • 63
  • 1
  • 7
  • 1
    I don't know unity, so I don't know if there is a static or instance method called `LoadScene` declared in `MonoBehaviour`. But the way you implemented your `SceneManager.LoadScene` method, it's not overriding something. It's just trying to call a _static_ `LoadScene` method of the `SceneManager`, but no such (_static_) method is defined. – René Vogt Aug 22 '19 at 15:12
  • No, there isn't a method called ```LoadScene```, that's just what I've called it myself to make it easier to use in the future. I think that's why I couldn't find any pre-existing help! – mreyeball Aug 22 '19 at 15:17
  • Unity already has a class named `SceneManager` with a method called [`SceneManager.LoadScene`](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html). However since you implemented your own class with the exact same name `SceneManager` .. the script "thinks" you are trying to call the method `LoadScene` of that one which is not `static` .... sintead simply use `SceneManagement.SceneManager.LoadScene(level)` .. or better ... why even implement your own `SceneManager` **at all** instead of simply using the static call? – derHugo Aug 22 '19 at 15:19
  • Okay, so I changed the method name to ```foo```, but how do I change the name of the class while keeping it understandable for the code to know what I want? (If that sounds stupid, know I'm not new to text programming but I am new to C#). – mreyeball Aug 22 '19 at 15:23
  • the main question is: Why using your custom script at all? Simply use the static `SceneManager.LoadScene` that already exists .. don't implement your own `SceneManager` – derHugo Aug 22 '19 at 15:24
  • Can you show me? I don't think I'll do it right :/ – mreyeball Aug 22 '19 at 15:25
  • see the [example in the link](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html): you have to have `using UnityEngine.SceneManagement;` on top of your script and then simply do `if (collision.gameObject.CompareTag("NextT")){SceneManager.LoadScene("Level1"); }` Btw you **never** instantiate MonoBehaviour components using `new` ;) – derHugo Aug 22 '19 at 15:27
  • Okay, so I now have realised something _really_ stupid. The reason I had to ask the question in the first place was that nothing happened when I touched the cube. I've just discovered that the reason it didn't work was that I hadn't actually tagged the cube... . Now I just have to add the next scene to Build Settings. Thanks! – mreyeball Aug 22 '19 at 15:33

0 Answers0