Can´t get damaged on DamageZone and then collect CollectibleHealth

Hi
so I have this problem -

My character after stepping on some spikes:

NullReferenceException: Object reference not set to an instance of an object
RubyController.ChangeHealth (System.Int32 amount) (at Assets/Scripts/RubyController.cs:101)
DamageZone.OnTriggerStay2D (UnityEngine.Collider2D other) (at Assets/Scripts/DamageZone.cs:14)

Then collecting health:

NullReferenceException: Object reference not set to an instance of an object
RubyController.ChangeHealth (System.Int32 amount) (at Assets/Scripts/RubyController.cs:101)
HealthCollectible.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/HealthCollectible.cs:15)

Here are my scripts:

Thanks for any help!

Main Character

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RubyController : MonoBehaviour
{
public float speed = 3.0f;

public int maxHealth = 5;

public GameObject projectilePrefab;

public int health { get { return currentHealth; } }
int currentHealth;

public float timeInvincible = 2.0f;
bool isInvincible;
float invincibleTimer;

Rigidbody2D rigidbody2d;
float horizontal;
float vertical;

Animator animator;
Vector2 lookDirection = new Vector2(1, 0);

// Start is called before the first frame update
void Start()
{
rigidbody2d = GetComponent();
animator = GetComponent();
currentHealth = maxHealth;
}

// Update is called once per frame
void Update()
{
horizontal = Input.GetAxis(“Horizontal”);
vertical = Input.GetAxis(“Vertical”);

Vector2 move = new Vector2(horizontal, vertical);

if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}

animator.SetFloat(“Look X”, lookDirection.x);
animator.SetFloat(“Look Y”, lookDirection.y);
animator.SetFloat(“Speed”, move.magnitude);

if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}

if (Input.GetKeyDown(KeyCode.C))
{
Launch();
}

if (Input.GetKeyDown(KeyCode.X))
{
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask(“NPC”));
if (hit.collider != null)
{
NonPlayerCharacter character = hit.collider.GetComponent();
if (character != null)
{
character.DisplayDialog();
}
}
}
}

void FixedUpdate()
{
Vector2 position = rigidbody2d.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
position.y = position.y + speed * vertical * Time.deltaTime;

rigidbody2d.MovePosition(position);
}

public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
return;

isInvincible = true;
invincibleTimer = timeInvincible;
}

currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);

UIHealthBar.instance.SetValue(currentHealth / (float)maxHealth);
}

void Launch()
{
GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);

Projectile projectile = projectileObject.GetComponent();
projectile.Launch(lookDirection, 300);

animator.SetTrigger(“Launch”);
}
}

DamageZone (spikes)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamageZone : MonoBehaviour
{

void OnTriggerStay2D(Collider2D other)
{
RubyController controller = other.GetComponent();

if (controller != null)
{
controller.ChangeHealth(-1);
}
}

}

and Health script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthCollectible : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
RubyController controller = other.GetComponent();

if (controller != null)
{
if (controller.health < controller.maxHealth)
{
controller.ChangeHealth(1);
Destroy(gameObject);
}
}

}
}

Well , i was going to see about assisting here, but…

Tip: , use code format, i just woke up , and i cant handle this mess. I assume most wont even bother reading past line 1.

1 Like

True, I will use code format next time. Thx

If you can’t help, don’t insult the OP for his post, or even better, don’t reply at all.

But yeah the OP would be better using code tags.

It wasn´t the nicest thing but I understand what he was talking about. It´s not the prettiest thing to read, so Yeah. :slight_smile:

Go to RubyController.cs, line 101. I can’t because there are no line numbers. Whatever is on line 101, figure out which references are used there. If there is only 1 reference, then that’s what is null. If there are multiple references, add debugging ahead of that line or split that line up so you can figure out which reference is null. Once you know which reference is null, either figure out why it is null so you can make it not null, or if it is sometimes supposed to be null then redesign your code so you don’t use that reference when it is null.

This is the same process you use whenever you hit null reference errors, and since it is one of the most common errors you will encounter, you’ll have to get good and troubleshooting it.