脚本变得很奇怪,有 20 个没有意义的错误(统一)

Script went weird with 20 errors which don't make sense (unity)

长话短说,我在我正在开发的游戏中添加了一些代码,但突然出现了 20 个错误,它们根本没有意义。至少在我看来,这是一个小故障,但我该如何解决它?

这是代码和错误(有更多的错误,但都差不多) 错误重定向到代码的随机部分,在任何情况下都不是错误,所以我真的很想知道为什么会这样,我不能再玩我的游戏了

     using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerCore : MonoBehaviour

{
    // Health Variables
    public int MaxHealth = 2;
    public int Health;
    // Other Variables
    private bool DelayCheck = true;


    // Part about taking damage and taking damage when colliding with platforms that do damage and level borders
    public void TakeDamage(int Damage)
    {
        Health = Health - Damage;
    }

    public void Die()
    {
        Destroy(this.gameObject);
    }

    IEnumerator DelayBetweenDamage()
     {
        DelayCheck = false;
        TakeDamage(1);
        Debug.Log("Player took 1 damage.");
        yield return new WaitForSecondsRealtime(1);
        DelayCheck = true;

    }

    // Take Damage when touching platforms with the DMG_platform tag
    void OnCollisionStay2D (Collision2D col)
    {
        if (col.gameObject.tag == "DMG_platform" && DelayCheck == true) 
        {
            StartCoroutine(DelayBetweenDamage());
        }


    }

    // Take fatal damages when falling or going out of the map borders
    void OnCollisionEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Level_Border")
        {
            TakeDamage(MaxHealth);
        }
    }

    // ----------------------------------------------------------------------------------------------------
    // Part about Bonus getting triggered and applying them to the player

    void OnTriggerEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);
            
            // if player touches Power Jump Bonus
            if (col.gameObject.name == "Power Jump")
            {
                \[SerializeField\] private Jump jump;
                jump.jumpVelocity = 22f;
            }
            
            // if player touches Power Health Bonus
            if (col.gameObject.name == "Power Health")
            {
                MaxHealth = 3;
                if (Health <= 2)
                {
                    Health = Health + 1;
                }
            }
        }
    }



    // Start is called before the first frame update
    void Start()
    {
        Health = MaxHealth;
        
    }

    // Update is called once per frame
    void Update()
    {
        // Dies if Health is less than 0 
        if (Health <= 0)
        {
            Die();
        }
        
        
    }




}

你破坏了语法结构。 我重新格式化了代码,这里是

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCore : MonoBehaviour

{
    // Health Variables
    public int MaxHealth = 2;

    public int Health;

    // Other Variables
    private bool DelayCheck = true;


    // Part about taking damage and taking damage when colliding with platforms that do damage and level borders
    public void TakeDamage(int Damage)
    {
        Health = Health - Damage;
    }

    public void Die()
    {
        Destroy(this.gameObject);
    }

    IEnumerator DelayBetweenDamage()
    {
        DelayCheck = false;
        TakeDamage(1);
        Debug.Log("Player took 1 damage.");
        yield return new WaitForSecondsRealtime(1);
        DelayCheck = true;
    }

    // Take Damage when touching platforms with the DMG_platform tag
    void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.tag == "DMG_platform" && DelayCheck == true)
        {
            StartCoroutine(DelayBetweenDamage());
        }
    }

    // Take fatal damages when falling or going out of the map borders
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Level_Border")
        {
            TakeDamage(MaxHealth);
        }
    }

    // ----------------------------------------------------------------------------------------------------
    // Part about Bonus getting triggered and applying them to the player

    void OnTriggerEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);

            // if player touches Power Jump Bonus
            if (col.gameObject.name == "Power Jump")
            {
                jump.jumpVelocity = 22f;
            }
            
            // if player touches Power Health Bonus
            if (col.gameObject.name == "Power Health")
            {
                MaxHealth = 3;
                if (Health <= 2)
                {
                    Health = Health + 1;
                }
            }
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Health = MaxHealth;
    }

    // Update is called once per frame
    void Update()
    {
        // Dies if Health is less than 0 
        if (Health <= 0)
        {
            Die();
        }
    }

    [SerializeField] private Jump jump;
}
void OnTriggerEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);
            
            // if player touches Power Jump Bonus
            if (col.gameObject.name == "Power Jump")
            {
                \[SerializeField\] private Jump jump;
                jump.jumpVelocity = 22f;
            }
            
            // if player touches Power Health Bonus
            if (col.gameObject.name == "Power Health")
            {
                MaxHealth = 3;
                if (Health <= 2)
                {
                    Health = Health + 1;
                }
            }
        }
    }

此代码有误,请改为:

if (col.gameObject.name == "Power Jump")
            {
                Jump jump;
                jump.jumpVelocity = 22f;
            }

您不能在方法中声明带有私有字段的变量。 此外,我建议您像声明健康变量一样声明此变量

下面的脚本已经修改和修复。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCore : MonoBehaviour
{
    [SerializeField] private Jump jump;
    
    // Health Variables
    public int MaxHealth = 2;
    public int Health;
    // Other Variables
    private bool DelayCheck = true;
    
    // Start is called before the first frame update
    void Start()
    {
        Health = MaxHealth;
    }

// Update is called once per frame
    void Update()
    {
        // Dies if Health is less than 0 
        if (Health <= 0)
        {
            Die();
        }
    }

    // Part about taking damage and taking damage when colliding with platforms that do damage and level borders
    public void TakeDamage(int Damage)
    {
        Health = Health - Damage;
    }

    public void Die()
    {
        Destroy(this.gameObject);
    }

    IEnumerator DelayBetweenDamage()
    {
        DelayCheck = false;
        TakeDamage(1);
        Debug.Log("Player took 1 damage.");
        yield return new WaitForSecondsRealtime(1);
        DelayCheck = true;
    }

    // Take Damage when touching platforms with the DMG_platform tag
    void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.tag == "DMG_platform" && DelayCheck == true)
        {
            StartCoroutine(DelayBetweenDamage());
        }
    }

    // Take fatal damages when falling or going out of the map borders
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Level_Border")
        {
            TakeDamage(MaxHealth);
        }
    }
    // ----------------------------------------------------------------------------------------------------
    // Part about Bonus getting triggered and applying them to the player
    void OnTriggerEnter2D(Collision2D col)
    {
        // if player touches Power Health Bonus
        if (col.gameObject.name == "Power Health")
        {
            MaxHealth = 3;
            if (Health <= 2)
            {
                Health = Health + 1;
            }
        }
        
        if (col.gameObject.tag == "Power")
        {
            Destroy(col.gameObject);
        }
        // if player touches Power Jump Bonus
        if (col.gameObject.name == "Power Jump")
        {
            jump.jumpVelocity = 22f;
        }
    }
}