簡體   English   中英

有 2 個錯誤,“開始”和“更新”錯誤 CS0111:類型“敵人”已經定義了一個名為“開始”的成員,具有相同的參數類型我該如何解決?

[英]Got 2 errors with 'start' and 'update' error CS0111: Type 'Enemy' already defines a member called 'Start' with the same parameter types how can i fix?

從 1 個腳本中得到 2 個錯誤,不知道發生了什么。 如果有人可以提供幫助,那就太好了。 錯誤在標題中,另一個是相同的,除了“開始”而不是“更新”

謝謝閱讀

using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float health = 50f;
    private Rigidbody rb;
    
    // Start is called before the first frame update
    void Start()
    {
        rb = this.GetComponent<Rigidbody>();
    }
    
    // Update is called once per frame
    void Update()
    {
        Vector3 direction = player.position - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        float y = Quaternion.identity.eulerAngles.y;
        float z = Quaternion.identity.eulerAngles.z;
        rb.rotation = Quaternion.Euler(angle, y, z);
    }
}

使用override運算符應該可以消除編譯器錯誤,但我不是 Unity 開發人員,所以我不確定 Unity 是否會以正確的方式處理您的 class。 以下是您的操作方法:

    override void Start()
    {
    ...
    }
    
    // Update is called once per frame
    override void Update()
    {
    ...
    }

您可以在此處閱讀有關覆蓋運算符的更多信息: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override

我還找到了這篇文章,它將Start聲明為IEnumerator而不是void ,因此也可以修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM