c# - 当玩家与激光碰撞时,生命值不会减少

标签 c# unity3d

每当玩家与敌方激光发生碰撞时,我都试图降低玩家的生命值,但我编写的脚本无法正常工作,并且出现以下错误:

Assets/Scripts/Laser/HealthManager.cs(19,21): error CS0029: Cannot implicitly convert type void' to UnityEngine.UI.Slider.

有人可以花点时间检查我的代码并告诉我为什么健康栏不工作吗?谢谢。

HealthManager 脚本:

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

public class HealthManager : MonoBehaviour {
public int CurrentHealth { get; set; }
public int MaxHealth { get; set; }
public Slider HealthBar;
//public GameObject LaserBulletEnemyPreFab;
//public GameObject PlayerPrefab;
//public LaserLevelManager myLevelmanager;

// Use this for initialization
void Start () {

    MaxHealth = 20;
    CurrentHealth = MaxHealth; //Resseting the health on start 
    HealthBar = CalculatingHealth();

}

// Update is called once per frame
void Update()
{
    if(GetComponent<Collider>().gameObject.tag == "EnemyLaser")
    {
        Destroy(GetComponent<Collider>().gameObject);
        DealDamage(1);
    }
}

void DealDamage(int DamageValue)
{
    CurrentHealth -= DamageValue; //Deduct the damage dealt from the player's health
    HealthBar = CalculatingHealth();
    if(CurrentHealth <= 0) //If health is 0 
    {
        PlayerDead(); //Calling the function
    }
}

void CalculatingHealth()
{
    int healthdecrease =  CurrentHealth / MaxHealth;
}

void PlayerDead()
{
    CurrentHealth = 0; //Currenthealth is 0
    LaserLevelManager.LoadLevel("Lose"); //Take player to the lose scene

   }


}

最佳答案

HealthBar 属于 Slider 类型。 CalculatingHealth 是一个不返回任何内容的函数(如此无效)。 您尝试将初始初始化为类型 Slider 的变量设置为 void 值。这是不可能的。

你可以:

float CalculatingHealth()
{
    return healthdecrease =  CurrentHealth / MaxHealth;
}

HealthBar.value = CalculatingHealth();

备注:https://docs.unity3d.com/ScriptReference/UI.Slider-value.html

关于c# - 当玩家与激光碰撞时,生命值不会减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56118068/

相关文章:

c# - 每个频率的响度相等

c# - 无法加密 Azure CloudFile。能够加密 Azure Blob

android - Unity手游卡顿+profiler帮助

android - GvrVideoPlayer.cs 错误阻止我编译应用程序。我所做的就是导入 GVR sdk!我怎样才能解决这个问题?

c# - 通过不使用检查器在 Unity 中重构代码

android - Vuforia 和 Unity 1.5 不在 Android 场景中渲染对象

c# - 如何在 Rhino Mocks 3.6 中将 Expect 设置为扩展方法

c# - 获取超时已过期。在从池中获取连接之前超时期限已过。 。异常(exception)

c# - 从路径读取事件日志文件

c# - 按下播放后,Unity序列化不同列表中的对象不相等