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