我正在Unity3D中制作2D平台游戏(您只能通过跳跃沿X轴和Y轴进行控制),并且我正试图通过加载下一个场景进入下一个阶段。
我尝试遍历整个堆栈溢出和其他站点,但找不到任何帮助。这是我的代码:
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;
}
}
}
消息是;
Assets \ Player.cs(10,9):错误CS0120:非静态字段,方法或属性'SceneManager.LoadScene(string)'需要对象引用
由于它是编译器错误,因此我无法启动游戏。顺便说一句,我真的是C#的新手,所以请忽略代码中的任何低效率之处。 :)