1

I'm using Microsoft Visual Studio.

I've been trying to make a "heating" script for an empty sprite with a CircleCollider2D, but everything I do doesn't work. I made a Freezing script so that the player would slowly freeze, and I wanted them to be able to replenish their "temp" with a campfire; via an aura around it. Basically a health bar, and a healing aura.

There isn't much about healing auras online, I could find a bit about Coroutines. One video on YouTube had exactly what I was looking for (https://www.youtube.com/watch?v=KE9BHGgVP4A). I found many tutorials on health pickups, but they don't help with what I'm trying to accomplish.

The code didn't work, so I tinkered with it to try and fix it. Now it's throwing the error, "NullReferenceException: Object reference not set to an instance of an object Warmth.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/Warmth.cs:19)" Although, I think their may be more errors than this one.

Here is the Code:

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
}

}

Just in case the problem is in the other script, I will post that too.

Here is the code for the "Freezing" script:

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

}

}

Thanks to anyone who helps! This project I'm working on is a little game I've been building to learn more about C# and Unity, which I've done most of the programming including the Freezing script without many online tutorials, but this one has had me stumped for the past two days; I decided it was time to ask for help.

So, if you'd like to take out the time to explain anything else I've done wrong, I'd be grateful.

ryeMoss
  • 4,303
  • 1
  • 15
  • 34
  • I don't see anything wrong with your script. It's likely that you are just trying to access a component that is not attached to your gameobject, hence the Null Reference. – ryeMoss Jan 25 '19 at 17:25
  • Well the `frz` is `null` when you try to access it. Is the `Freeze` component attached to exactly same GameObject as `Warmth`? Not maybe lower on the hierarchy (than use `GetComponentOnChildren(true)`) or higher (than use `GetComponentInParent`)? Also `Player = GetComponent(); ` makes absolutely no sense. `GameObject` isn't a component. Is it possible that `OnTriggerEnter` is called before `Start` (unlikely but who knows)? – derHugo Jan 25 '19 at 17:43
  • @derHugo The Freeze component is attached to the Player. I wasn't sure if GameObject was a component, but I never thought to check. – Mark_Midnight Jan 25 '19 at 18:00
  • @derHugo Yep, that was my problem. I added Player. to the GetComponent of frz, now it appears to be working properly. Thanks! – Mark_Midnight Jan 25 '19 at 18:07
  • @Mark_Midnight glad you could solve it – derHugo Jan 25 '19 at 18:13

0 Answers0