Метод недоступен из-за уровня защиты - PullRequest
0 голосов
/ 19 октября 2018

В настоящее время я работаю в 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;
    }
}

Когда я пытаюсь использовать указанный метод в файле сценариев для врагаЯ получаю сообщение об ошибке: «Активы / Пользовательские сценарии / BeetleScript.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");
        }
    }  

 }
}

Любая помощь, чтобы исправить это будет приветствоваться.

Ответы [ 2 ]

0 голосов
/ 19 октября 2018

Вы должны сделать void ReduceHealth () публичным -> public void ReduceHealth ()

0 голосов
/ 19 октября 2018

Ваши методы private.Вы должны написать public перед методом, к которому вы хотите обратиться извне класса.

public void ReduceHealth()
{
...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...