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);
}
}

}
}

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

In the future, when you post code, please use code tags: Using code tags properly

1 Like

Counting to line 101 without including line numbers is a big ask for random people trying to help on the interwebs. Use code tags makes that automatic, or at least point out which line it is. But Kurt’s got the right link of course. You figure out what is null on whatever line 101 is. If more than 1 reference is used on that line, then add debugging or break that line out into multiple lines to narrow it down to a specific reference. After you know what is null, figure out why it isn’t being set before you try to use it.

1 Like