我目前正在 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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在敌人的脚本文件中使用上述方法时,出现错误,指出“Assets/Custom Scripts/BeetleScript.cs(46,28): error CS0122: `HealthManager.ReduceHealth()' 由于其保护而无法访问等级”
以下是敌人脚本启动正在使用的变量并调用方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeetleScript : MonoBehaviour
{
Animator animator;
public GameObject cucumberToDestroy; …
Run Code Online (Sandbox Code Playgroud)