Не видеть public int c# unity

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

public class Health : MonoBehaviour
{
    public int health;
    public int maxHealth;

    public void TakeHit(int damage)
    {
        health -= damage;

        if(health <= 0)
        {
            GetComponent<HeroController>().GameOver();
        }
    }

    public void SetHealth(int bonusHealth)
    {
        health += bonusHealth;

        if(health > maxHealth)
        {
            health = maxHealth;
        }
    }
}

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

public class EnemyAttack : MonoBehaviour
{

    public int collisionDamage = 10;
    public string collisionTag;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == collisionTag)
        {
            Health.health = collision.gameObject.GetComponent<Health>();
            health.TakeHit(damage);
        }
    }
}

Вдругом коде не видеть public int health а видет только public class Health


Ответы (1 шт):

Автор решения: CrazyElf

Ну тут явная ошибка, точка тут лишняя:

            Health.health = collision.gameObject.GetComponent<Health>();
                 ^^^
            health.TakeHit(damage);

Видимо имелось в виду не поле health объекта Health, а собственно сам объект типа Health:

            Health health = collision.gameObject.GetComponent<Health>();
            health.TakeHit(damage);
→ Ссылка