为什么“加热光环”的此脚本不起作用? Unity 2D C#

时间:2019-01-25 16:32:26

标签: c# unity3d 2d coroutine

我正在使用Microsoft Visual Studio。

我一直在尝试使用CircleCollider2D为空的精灵制作一个“加热”脚本,但是我做的所有事情都不起作用。我制作了一个Freezing脚本,以便玩家慢慢冻结,我希望他们能够用篝火补充他们的“临时”;通过它周围的光环。基本上是一个健康酒吧和一个治愈光环。

关于在线治疗光环的内容不多,我可以找到一些有关协程的信息。 YouTube上的一部视频正是我想要的(https://www.youtube.com/watch?v=KE9BHGgVP4A)。我找到了许多有关健康保险的教程,但它们对我要完成的工作没有帮助。

该代码无法正常工作,因此我对其进行了修补以尝试对其进行修复。现在抛出错误“ NullReferenceException:对象引用未设置为对象Warmth.OnTriggerEnter2D(UnityEngine.Collider2D col)的实例(在Assets / Scripts / Warmth.cs:19处)” 虽然,我认为他们可能比这个错误更多。

以下是代码:

using System.Collections;

using UnityEngine;

public class Warmth : MonoBehaviour
{
// Refrence to Freezing component (Basically the players "Health")
private Freezing frz;
public GameObject Player; // Reference to Player component

private void Start()
{
    // Shortens GetComponent to frz (Getting component "Freezing", to get "currentTemp")
    frz = GetComponent<Freezing>();
    Player = GetComponent<GameObject>(); // Shortens GetComponent to Player
}

void OnTriggerEnter2D(Collider2D col) // When Collider enters GameObject
{
    // If GameObject's name is equal to Player, and currentTemp is less then 100
    if (col.gameObject.name.Equals("Player") && frz.currentTemp < 100)
    { StartCoroutine("Warm"); } // Start Coroutine Warm
}

void OnTriggerExit2D(Collider2D col) // When Collider exits GameObject
{
    // If GameObject's name is equal to Player
    if (col.gameObject.name.Equals("Player"))
    { StopCoroutine("Warm"); } // Stop Coroutine Warm
}

IEnumerator Warm()
{
    // Check currentTemp's current amount, while currentTemp is less than or equal to 100, add value 1
    for (float currentTemp = frz.currentTemp; currentTemp <= 100; currentTemp += 1f)
    {
        frz.currentTemp = currentTemp;
        // Time.deltaTime slows each loop
        yield return new WaitForSeconds(Time.deltaTime);
    }
    frz.currentTemp = 100; // Sets Temp to 100 at the end of the loop
}

}

以防其他脚本出现问题,我也将其发布。

以下是“冻结”脚本的代码:

using UnityEngine;

using UnityEngine.UI;

public class Freezing : MonoBehaviour
{

public float startingTemp = 100; //Starting Temp
public float currentTemp;  //Current Temp
public Slider tempSlider; //Refrence for TempatureBar

private bool isFroze; //To check if player froze to death
private Animator anim; //Reference to the Animator component.
private PlayerController pc; //Refrence to the PlayerController component

private void Start()
{
    // Shortens GetComponent to anim.
    anim = GetComponent<Animator>();
    // Shortens GetComponent to pc
    pc = GetComponent<PlayerController>();
}

void Awake()
{
    // Set the initial temp of the player.
    currentTemp = startingTemp;
}

private void Update()
{
    if (isFroze == false) //Makes sure player isn't already dead
    { currentTemp -= Time.deltaTime; } //Minus player temp by deltatime

    // Set the temp bar's value to the current temp.
    tempSlider.value = currentTemp;

    if (currentTemp < 0) //Checks if temp is 0
    {
        //Enables death animation
        isFroze = true;
        //Disables PlayerController
        pc.enabled = false;
    }

    anim.SetBool("isFroze", isFroze); //Set isFroze to isFroze in the animator

}

}

感谢任何帮助!我正在从事的这个项目是我正在制作的一款小游戏,旨在了解有关C#和Unity的更多信息,我已经完成了大部分编程工作,包括Freezing脚本,但没有许多在线教程,但是这个让我很困惑过去两天;我认为是时候寻求帮助了。

因此,如果您想抽出时间解释我做错的其他事情,我将不胜感激。

0 个答案:

没有答案