OnCollisionEnter2D 问题 - 没有错误消息但没有结果

Issue with OnCollisionEnter2D - No error messages but no result

第一次在这里发帖,如有不妥之处请见谅。我写了一大段代码,用于检查附加对象是否正在接触另一个带有“危险”标签的对象。如果是,则浮点数“hp”的值将减少 1。当我 运行 我的游戏时没有错误消息,但是当我 运行 我的游戏并触摸标记为“危险”的对象时我的玩家角色 hp 没有受到影响 - 我知道这一点是因为我成功地实现了一个健康栏,当我触摸带有标签的对象时它根本不会改变。

下面是我编写的检测碰撞并降低hp值的代码。

void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag =="hazard")
        {
          hp = hp - 1;
        }
    }

玩家角色对象和危险对象都附加了 2D Collider 组件。

应要求,健康码已添加如下:

此代码设置“hp”的初始值并声明我稍后用来表示玩家剩余生命值的游戏对象。

float hp = 10;
   public GameObject phealth1;
   public GameObject phealth2;
   public GameObject phealth3;
   public GameObject phealth4;
   public GameObject phealth5;
   public GameObject phealth6;
   public GameObject phealth7;
   public GameObject phealth8;
   public GameObject phealth9;
   public GameObject phealth10;

此代码可防止“hp”高于或低于最大值和最小值,以及根据“hp”的值更新健康栏。

 void Update()
{
    
    if (hp > 10)
        hp = 10;

    if (hp < 0)
        hp = 0;

    switch (hp) {
        case 10:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(true);
            phealth7.gameObject.SetActive(true);
            phealth8.gameObject.SetActive(true);
            phealth9.gameObject.SetActive(true);
            phealth10.gameObject.SetActive(true);
            break;
        case 9:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(true);
            phealth7.gameObject.SetActive(true);
            phealth8.gameObject.SetActive(true);
            phealth9.gameObject.SetActive(true);
            phealth10.gameObject.SetActive(false);
            break;
        case 8:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(true);
            phealth7.gameObject.SetActive(true);
            phealth8.gameObject.SetActive(true);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 7:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(true);
            phealth7.gameObject.SetActive(true);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 6:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(true);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 5:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(true);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 4:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(true);
            phealth5.gameObject.SetActive(false);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 3:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(true);
            phealth4.gameObject.SetActive(false);
            phealth5.gameObject.SetActive(false);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 2:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(true);
            phealth3.gameObject.SetActive(false);
            phealth4.gameObject.SetActive(false);
            phealth5.gameObject.SetActive(false);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 1:
            phealth1.gameObject.SetActive(true);
            phealth2.gameObject.SetActive(false);
            phealth3.gameObject.SetActive(false);
            phealth4.gameObject.SetActive(false);
            phealth5.gameObject.SetActive(false);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
        case 0:
            phealth1.gameObject.SetActive(false);
            phealth2.gameObject.SetActive(false);
            phealth3.gameObject.SetActive(false);
            phealth4.gameObject.SetActive(false);
            phealth5.gameObject.SetActive(false);
            phealth6.gameObject.SetActive(false);
            phealth7.gameObject.SetActive(false);
            phealth8.gameObject.SetActive(false);
            phealth9.gameObject.SetActive(false);
            phealth10.gameObject.SetActive(false);
            break;
    }

}

下面是整个脚本。

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

public class Health : MonoBehaviour
{
   float hp = 10;
   public GameObject phealth1;
   public GameObject phealth2;
   public GameObject phealth3;
   public GameObject phealth4;
   public GameObject phealth5;
   public GameObject phealth6;
   public GameObject phealth7;
   public GameObject phealth8;
   public GameObject phealth9;
   public GameObject phealth10;
   
    // Start is called before the first frame update
    void Start()
    {
        
    }

    void dmg(int x)
    {
        hp = hp - x;
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "hazard")
            dmg(1);
            Debug.Log("Contact made");
            Debug.Log(hp);
    }

    // Update is called once per frame
    void Update()
    {

        if (hp > 10)
            hp = 10;

        if (hp < 0)
            hp = 0;

        switch (hp) {
            case 10:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(true);
                phealth7.gameObject.SetActive(true);
                phealth8.gameObject.SetActive(true);
                phealth9.gameObject.SetActive(true);
                phealth10.gameObject.SetActive(true);
                break;
            case 9:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(true);
                phealth7.gameObject.SetActive(true);
                phealth8.gameObject.SetActive(true);
                phealth9.gameObject.SetActive(true);
                phealth10.gameObject.SetActive(false);
                break;
            case 8:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(true);
                phealth7.gameObject.SetActive(true);
                phealth8.gameObject.SetActive(true);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 7:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(true);
                phealth7.gameObject.SetActive(true);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 6:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(true);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 5:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(true);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 4:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(true);
                phealth5.gameObject.SetActive(false);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 3:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(true);
                phealth4.gameObject.SetActive(false);
                phealth5.gameObject.SetActive(false);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 2:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(true);
                phealth3.gameObject.SetActive(false);
                phealth4.gameObject.SetActive(false);
                phealth5.gameObject.SetActive(false);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 1:
                phealth1.gameObject.SetActive(true);
                phealth2.gameObject.SetActive(false);
                phealth3.gameObject.SetActive(false);
                phealth4.gameObject.SetActive(false);
                phealth5.gameObject.SetActive(false);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
            case 0:
                phealth1.gameObject.SetActive(false);
                phealth2.gameObject.SetActive(false);
                phealth3.gameObject.SetActive(false);
                phealth4.gameObject.SetActive(false);
                phealth5.gameObject.SetActive(false);
                phealth6.gameObject.SetActive(false);
                phealth7.gameObject.SetActive(false);
                phealth8.gameObject.SetActive(false);
                phealth9.gameObject.SetActive(false);
                phealth10.gameObject.SetActive(false);
                break;
        }

    }
    
    
}

我怀疑现场有问题

  1. 检查场景中是否只有一个脚本

In the Hierarchy Tab type in the search bar t:MyComponentName. Multiple copies using the same phealth objects could cause the issue you are experiencing.

  1. 仔细检查检查器中分配的 phealth{n} 游戏对象。

这是您的代码的清理版本。测试并确认工作。

using UnityEngine;

public class Health : MonoBehaviour
{
    public int health;
    public int maxHealth;
    public GameObject[] healthObjs;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("hazard"))
        {
            AddHealth(-1);
        }
    }

    private void AddHealth(int amount)
    {
        health = Mathf.Max(0, Mathf.Min(maxHealth, health + amount));

        UpdateDisplayedHealth();
    }
    private void UpdateDisplayedHealth()
    {
        for (int i = 0; i < healthObjs.Length; i++)
        {
            healthObjs[i].SetActive(i + 1 <= health);
        }
    }
}
  1. 删除了 switch case,将其替换为 for 循环,使代码更加紧凑。
  2. 更新了损坏函数以处理 min/max 生命值并更新显示的生命值。
  3. 完全删除更新。