碰撞编号 2 处的错误标记

Wrong tag at collision number 2

你好,我正在制作一个 2d 游戏,我有一个玩家需要与 2 个物体发生碰撞。第一个对象给了他更多的健康并且有标签“power up”并且可以工作但是第二个有标签“Hurt”它不工作。我应该在脚本中更改什么?这是“PowerUp”的脚本,这是播放器的脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PowerUpDetector : MonoBehaviourPun
{
    // reference this via the Inspector
    [SerializeField] private Character healthbar;
    [SerializeField] private Character health;
   

    private void Awake()
    {
        if (!healthbar) healthbar = GetComponent<Character>();
        
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // or whatever tag your powerups have
        if (!other.CompareTag("PowerUp"))
        {
            Debug.LogWarning($"Registered a collision but with wrong tag: {other.tag}", this);
            return;
        }
        
        var powerup = other.GetComponent<PowerUp>();
        if (!powerup)
        {
            Debug.LogError($"Object {other.name} is tagged PowerUp but has no PowerUp component attached", this);
            return;
        }

        Debug.Log("Found powerup, pick it up!", this);
        powerup.Pickup(healthbar);
        if (!other.CompareTag("Hurt"))
        {
            if (photonView.IsMine)
            {
                photonView.RPC("Damage", RpcTarget.All);
                
            }

            
        }



    }
    [PunRPC]
    void Damage()
    {
        health -= 20;
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using Photon.Pun;
using Photon.Realtime;
using Photon;
using UnityEngine.UI;


public class Character : MonoBehaviourPun,IPunObservable
{

    Rigidbody2D rb;
    float dirX;

    [SerializeField]
    float moveSpeed = 5f, jumpForce = 400f, bulletSpeed = 500f;

    [SerializeField] private float health = 100;
    [SerializeField] private Slider slider;
    [SerializeField] private Gradient gradient;
    [SerializeField] private Image fill;

    Vector3 localScale;
   
    public Transform barrel;
    public Rigidbody2D bullet;

    // Use this for initialization
    void Start()
    {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D>();
        if (photonView.IsMine)
        {
            
            
        }
        else
        {

        }
    }
    public float Health
    {
        get { return health; }
        set
        {
            health = value;
            slider.value = health;
            fill.color = gradient.Evaluate(slider.normalizedValue);
        }
    }

    private void OnCollisionEntered2D(Collision2D col)
    {
        if( col.gameObject.CompareTag ("Hurt"))
        {
            if (photonView.IsMine)
            {
                photonView.RPC("Damage", RpcTarget.All);
            }
        }
    }
    [PunRPC]
    void Damage()
    {
        health -= 20;
    }
    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            dirX = CrossPlatformInputManager.GetAxis("Horizontal");
            if (dirX != 0)
            {
                barrel.up = Vector3.right * Mathf.Sign(dirX);
            }

            if (CrossPlatformInputManager.GetButtonDown("Jump"))
                Jump();

            if (CrossPlatformInputManager.GetButtonDown("Fire1"))
                Fire();
            
        }
        else
        {

        }
    }

    void FixedUpdate()
    {
        if (photonView.IsMine)
        {
            rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
        }
    }




    void Jump()
    {
        if (photonView.IsMine)
        {
            if (rb.velocity.y == 0)
                rb.AddForce(Vector2.up * jumpForce);
        }
    }

    void Fire()
    {
        if (photonView.IsMine)
        {
            var firedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
            firedBullet.AddForce(barrel.up * bulletSpeed);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(health);
        }else if (stream.IsReading)
        {
           health = (float)stream.ReceiveNext();
        }
    }
    public void SetMaxHealth(int value)
    {
        if (photonView.IsMine)
        {
            slider.maxValue = value;
            // The property handles the rest anyway
            Health = value;
        }

    }
}

我在 health -= 20 的 "Power Up Detector" 中有一个错误(运算符 -= 不能应用)到操作数类型 "Character" 和“int”。

我认为您将不得不使用 health.Health。您的变量 health 指的是 Character class。因为你想改变健康,你必须使用 class 的 属性 来代替。所以尝试:

health.Health -= 20;

我建议将 health 变量重命名为与 Character class 相关的其他名称。