Здоровье не уменьшается, когда игрок сталкивается с лазером - PullRequest
2 голосов
/ 13 мая 2019

Я пытаюсь уменьшить панель здоровья игрока, когда он сталкивается с вражеским лазером, но написанный мной скрипт не работает и выдает следующую ошибку:

Активы / Скрипты/Laser/HealthManager.cs(19,21): ошибка CS0029: невозможно неявное преобразование типа void 'в 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

   }


}

1 Ответ

3 голосов
/ 13 мая 2019

HealthBar имеет тип Slider.CalculatingHealth - это функция, которая ничего не возвращает (так что void).Вы пытаетесь установить переменную, инициализированную для типа Slider, в значение void.Это невозможно.

Вы можете:

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

И

HealthBar.value = CalculatingHealth();

Примечание: https://docs.unity3d.com/ScriptReference/UI.Slider-value.html

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