C# 方法由于其保护级别而不可访问

C# 方法由于其保护级别而不可访问,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我目前正在Unity 2018中开发,并制作了一个脚本,用于在与敌人碰撞时降低角色的生命值: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthManager : MonoBehaviour { public static int currentHealth; public Slider

我目前正在Unity 2018中开发,并制作了一个脚本,用于在与敌人碰撞时降低角色的生命值:

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

public class HealthManager : MonoBehaviour
{

    public static int currentHealth;
    public Slider healthBar;

    void Awake()
    {
        healthBar = GetComponent<Slider> ();
        currentHealth = 100;
    }

    void ReduceHealth()
    {
        currentHealth = currentHealth - 1;
        healthBar.value = currentHealth;
    }

    void Update()
    {
        healthBar.value = currentHealth;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类HealthManager:MonoBehavior
{
公共卫生;
公共健康酒吧;
无效唤醒()
{
healthBar=GetComponent();
currentHealth=100;
}
无效还原健康()
{
currentHealth=currentHealth-1;
healthBar.value=currentHealth;
}
无效更新()
{
healthBar.value=currentHealth;
}
}
当我试图在为敌人编写的脚本文件中使用上述方法时,我收到一个错误,说明“Assets/Custom Scripts/belletscript.cs(46,28):错误CS0122:`HealthManager.ReduceHealth()'由于其保护级别而无法访问”

以下是启动所用变量并调用方法的脚本:

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

public class BeetleScript : MonoBehaviour
{

Animator animator;
public GameObject cucumberToDestroy;
public bool cherryHit = false;
public float smoothTime = 3.0f;
public Vector3 smoothVelocity = Vector3.zero;
public PointsManager _ptsManager;
public HealthManager _healthManager;

void Start()
{
    animator = GetComponent<Animator>();
}

void Update()
{
    if (cherryHit)
    {

        var cm = GameObject.Find("CucumberMan");
        var tf = cm.transform;
        this.gameObject.transform.LookAt(tf);

        // move towards Cucumber Man
        animator.Play("Standing Run");

        transform.position = Vector3.SmoothDamp(transform.position, tf.position,
            ref smoothVelocity, smoothTime);
    }
}

// Collision Detection Test
void OnCollisionEnter(Collision col)
{
    if (col.gameObject.CompareTag("Player"))
    {

        _healthManager = GameObject.Find
        ("Health_Slider").GetComponent<HealthManager>();
        _healthManager.ReduceHealth();

        if (!cherryHit)
        {

            BeetlePatrol.isAttacking = true;

            var cm = GameObject.Find("CucumberMan");
            var tf = cm.transform;
            this.gameObject.transform.LookAt(tf);

            animator.Play("Attacking on Ground");
            StartCoroutine("DestroySelfOnGround");
        }
        else
        {
            animator.Play("Standing Attack");
            StartCoroutine("DestroySelfStanding");
        }
    }  

 }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类脚本:单行为
{
动画师;
公众游戏对象黄瓜雌雄同体;
public bool cherryHit=false;
公共浮动平滑时间=3.0f;
公共向量3平滑速度=向量3.0;
公共点管理器_ptsManager;
公共卫生经理(u HealthManager),;
void Start()
{
animator=GetComponent();
}
无效更新()
{
如果(樱桃红)
{
var cm=GameObject.Find(“黄瓜人”);
var-tf=cm.transform;
this.gameObject.transform.LookAt(tf);
//走向黄瓜人
动画师。播放(“站立跑步”);
transform.position=Vector3.SmoothDamp(transform.position,tf.position,
参考平滑速度、平滑时间);
}
}
//碰撞检测试验
无效碰撞中心(碰撞柱)
{
if(col.gameObject.CompareTag(“玩家”))
{
_healthManager=GameObject.Find
(“健康滑块”).GetComponent();
_healthManager.ReduceHealth();
如果(!cherryHit)
{
beletPatrol.isAttacking=true;
var cm=GameObject.Find(“黄瓜人”);
var-tf=cm.transform;
this.gameObject.transform.LookAt(tf);
动画师。播放(“攻击地面”);
STARTCOUNTROUNT(“销毁自我循环”);
}
其他的
{
动画师。播放(“站立攻击”);
STARTCOUTROUTION(“自立”);
}
}  
}
}

如果您能帮助解决此问题,我们将不胜感激。

您的方法是私有的。 您必须在要从类外访问的方法前面编写
public

public void ReduceHealth()
{
...
}

您需要将void ReduceHealth()设置为public->public void ReduceHealth()

将方法设置为public,默认情况下它们是私有的,因此将
void ReduceHealth()
更改为
public void ReduceHealth()
@LasseVågsætherKarlsen这不是注释。这是一个答案。你应该把它写下来作为回答我觉得自己像个白痴哈哈,不知道我怎么会错过这个。非常感谢!