我正在学习如何使用 Unity 作为引擎开发 2D 平台游戏并在 Microsoft VisualStudioCode(紫色)中编写脚本的教程。
在遵循教程时,我在 Unity 控制台中遇到了 2 个错误代码,但其中一个错误代码在 3 个不同的行中出现了 3 个不同的时间,这就是他们所说的。
错误代码1 (CS0101) “Assets\Scripts\PlayerMovement.cs(5,14):错误 CS0101:命名空间 '' 已包含 'PlayerMovement' 的定义”
错误代码2 (CS0111) “Assets\Scripts\PlayerMovement.cs(17,18):错误 CS0111:类型“PlayerMovement”已经定义了一个名为“Start”且具有相同参数类型的成员”
错误代码3 (CS0111) “Assets\Scripts\PlayerMovement.cs(28,18):错误 CS0111:类型“PlayerMovement”已经定义了一个名为“Update”且具有相同参数类型的成员”
错误代码 4 (CS0111) “Assets\Scripts\PlayerMovement.cs(46,22):错误 CS0111:类型“PlayerMovement”已经定义了一个名为“updateAnimationState”且具有相同参数类型的成员”
这是脚本的编码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Instiance Variables
private Rigidbody2D rb;
private SpriteRenderer sprite;
private Animator anim;
private float dirX = 0f;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 7f;
// Start is called before the first frame update
private void Start()
{
// Assign Instiance Variables to their respective refrences in Unity
// by using "GetComponet"
Debug.Log("Hello World!");
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sprite = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
private void Update()
{
// Player Directional Movement
dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
// Calls method to update animations
updateAnimationState();
}
private void updateAnimationState()
{
// Selection Statements that determines when to switch between using
// Idle animation or Running animation
if (dirX > 0f)
{
sprite.flipX = false;
}
else if (dirX < 0f)
{
sprite.flipX = true;
}
else
{
}
}
}
脚本代码结束
我不知道出了什么问题,请有人帮忙,这样我就可以完成这个该死的游戏。
我已经查找了 CS0111 和 CS0101 来查找导致错误的原因,但无法真正将导致这些错误的原因与我的代码联系起来。 简而言之,我不明白是什么导致了这些代码,也不明白如何修复它,走到这一步是最后的手段,现在我受够了,请有人帮助我。
错误表明您有另一个完全相同的类,因为类名和所有方法都匹配。您可能不小心将此类复制到其他地方。尝试找到并删除它。