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. :)