gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:28:44 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com