所以我是 Unity 的新手,我一直在尝试使用附加到角色的脚本来测试场景。但是,它一直显示“无法加载关联的脚本。请修复所有编译错误并分配有效的脚本。” 它还表示文件的名称可能与代码中的名称不同,但事实并非如此,并且还表示代码可能缺少 MonoBehaviour 脚本。它甚至不允许我将脚本附加到角色上,因为它找不到脚本类。
我从互联网上复制并下载了角色移动代码,但它们也不起作用。我也尝试过删除并重新制作 CS 文件,但这也不起作用。即使向角色添加空脚本也不起作用,除非我从“添加组件”中执行此操作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
SpriteRenderer sprite;
Rigidbody2D rigid;
// Start is called before the first frame update
void Start()
{
sprite = GetComponent<SpriteRenderer>();
rigid = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetKey("d"))
rigid.velocity = new Vector2(2, 0);
else if (Input.GetKey("a"))
rigid.velocity = new Vector2(-2, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
Unity 中也存在这些错误(如果有帮助的话)