Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity-Zelda life系统在更新hp容器时崩溃_C#_Unity3d - Fatal编程技术网

C# Unity-Zelda life系统在更新hp容器时崩溃

C# Unity-Zelda life系统在更新hp容器时崩溃,c#,unity3d,C#,Unity3d,我用5个健康容器建立了一个塞尔达健康系统。每个容器有4件 更新此healthbar时,我得到一个超出范围的异常 我的代码: [SerializeField] Image[] healthContainers; // 5 containers as images int currentHealth; int maxHealth; int healthPerHealthContainer = 4; // 1 healthcontainer = 4 health pieces private vo

我用5个健康容器建立了一个塞尔达健康系统。每个容器有4件

更新此healthbar时,我得到一个超出范围的异常

我的代码:

[SerializeField]
Image[] healthContainers; // 5 containers as images

int currentHealth;
int maxHealth;
int healthPerHealthContainer = 4; // 1 healthcontainer = 4 health pieces

private void Start()
{
    maxHealth = healthContainers.Length * healthPerHealthContainer; // set max hp
    currentHealth = maxHealth; // init current hp
    UpdateHealthBar(); // first gui update
}

public void ChangeHealth(int amount) // get damage or heal
{
    currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth); // add or subtract health
    UpdateHealthBar(); // update gui
}

 public void UpdateHealthBar() // gui updater
{
    int healthContainerIndex = currentHealth / healthPerHealthContainer; // get the current heart to change
    int healthContainerFillAmount = currentHealth % healthPerHealthContainer; // the fillamount of the heart

    if (currentHealth % healthPerHealthContainer == 0)
    {
        if (currentHealth == healthContainers.Length)
        {
            healthContainers[healthContainerIndex - 1].fillAmount = 1;
            return;
        }

        if (healthContainerIndex > 0)
        {
            healthContainers[healthContainerIndex].fillAmount = 0;
            healthContainers[healthContainerIndex - 1].fillAmount = 1;
        }
        else
        {
            healthContainers[healthContainerIndex].fillAmount = 0;
            return;
        }
    }

    healthContainers[healthContainerIndex].fillAmount = healthContainerFillAmount / (float)healthPerHealthContainer;
}
因此,当使用5个健康容器,每个健康容器4个时,我从20马力开始。更新gui时,它会在

healthContainers[healthContainerIndex].fillAmount = 0;
因为

int healthContainerIndex = currentHealth / healthPerHealthContainer;
导致

healthContainerIndex = 5;
包含5个容器的数组的索引范围为0到4

但仅仅是写作

healthContainers[healthContainerIndex -1].fillAmount = 0;

这是错误的,因为我需要得到正确的心脏。有人知道我在这里犯的错误吗?我看不出来。

我完全同意Jon Skeet的观点,把它写在一张纸上,这确实有助于解决像这样的索引问题

尽管如此,您可能需要重新考虑您的解决方案是否能按您希望的方式工作。你的解决方案只适用于假设玩家的生命值一次只上升或下降1个生命值的情况。如果玩家受到足够的伤害或完全恢复健康,你的解决方案可能会跳过一步,心脏可能无法正确更新。如果您只是迭代所有容器并更新它们,那么实现起来就会容易得多,而且更加健壮

public void UpdateHealthBar() // gui updater
{
    for (int healthContainerIndex = 0; healthContainerIndex < healthContainers.Length; healthContainerIndex++)
    {
        healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
    }
}
public void UpdateHealthBar()//gui更新程序
{
对于(int-healthContainerIndex=0;healthContainerIndex
绝对同意Jon Skeet将其写在一张纸上的观点,这确实有助于解决此类索引问题

尽管如此,您可能需要重新考虑您的解决方案是否能按您希望的方式工作。你的解决方案只适用于假设玩家的生命值一次只上升或下降1个生命值的情况。如果玩家受到足够的伤害或完全恢复健康,你的解决方案可能会跳过一步,心脏可能无法正确更新。如果您只是迭代所有容器并更新它们,那么实现起来就会容易得多,而且更加健壮

public void UpdateHealthBar() // gui updater
{
    for (int healthContainerIndex = 0; healthContainerIndex < healthContainers.Length; healthContainerIndex++)
    {
        healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
    }
}
public void UpdateHealthBar()//gui更新程序
{
对于(int-healthContainerIndex=0;healthContainerIndex
我建议你(在一张纸上)写下
currentHealth
的值和你期望的
healthContainerIndex
是什么,以及你期望更改的
healthContainers
索引。几乎没有理由统一使用[SerializedField]。@Fattie well writing public。。。不是很好。将变量设置为private似乎更干净我建议您(在一张纸上)写下
currentHealth
的值,以及您希望
healthContainerIndex
是什么,以及您希望更改的
healthContainers
索引。几乎没有理由使用[Serialized Field]在团结中,我写得很好。。。不是很好。将变量设置为private似乎更干净