Hi all,
I’m having a problem that I cant solve for literally days. I have a bullet with a script that when it collides with an enemy with the tag “Enemy”, it should get the DealDamage from that gameobject (the enemy) with the EnemyHealth script.
I’ve done this before in the past but when im doing the same thing now, I keeps giving me a “NullReferenceException: Object reference not set to an instance of an object” error. I think it should mean that it cannot locate the script that its searching but its not there… but it is? Its giving me a headache!
Please help me
It gives me the error on line 38.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAmmoAssaultRifle : MonoBehaviour
{
PlayerStats playerStats;
private float BulletCount = 30; // Each bullet does the damage of 1 / 30 of total damage.
private float Damage;
private float CritChance;
private float CritChanceRoll;
private float CritChanceMin = 0;
private float CritChanceMax = 101;
public GameObject bloodSplatter;
// EnemyHealth enemyHealth;
void Awake()
{
playerStats = GameObject.FindObjectOfType<PlayerStats>();
Damage = playerStats.damage / BulletCount;
CritChance = playerStats.playerTotalCritChance;
Destroy(gameObject, 3f);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
collision.gameObject.GetComponent<EnemyHealth>().DealDamage(Damage);
Instantiate(bloodSplatter, this.transform.position, this.transform.rotation);
CritChanceRoll = (Random.Range(CritChanceMin, CritChanceMax));
if(CritChanceRoll <= CritChance)
{
Damage *= 2;
Debug.Log("Critical Damage of = " + Damage + " !!!");
}
else
{
Debug.Log(Damage);
Destroy(gameObject);
}
}
else if(collision.gameObject)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public float health;
public float maxHealth;
public GameObject healthBarUI;
public Slider slider;
private string enemyTitle;
public Text enemyTitleText;
LootDrop lootDrop;
EnemyStats enemyStats;
Transform player;
private void Start()
{
enemyStats = GetComponent<EnemyStats>();
health = maxHealth;
slider.value = CalculateHealth();
player = GameObject.FindGameObjectWithTag("Player").transform;
enemyTitle = enemyStats.enemyName.ToString();
enemyTitleText.text = enemyTitle.ToString();
lootDrop = GetComponent<LootDrop>();
}
private void Update()
{
slider.transform.LookAt(player);
enemyTitleText.transform.LookAt(2 * enemyTitleText.transform.position - player.transform.position); // (2 * enemyTitleText.transform.position - player.transform.position) <<< this makes the Text go Backwards!!!!!
slider.value = CalculateHealth();
if(health < maxHealth)
{
healthBarUI.SetActive(true);
}
if(health <= 0)
{
lootDrop.OnDeath();
Destroy(gameObject);
}
if(health >= maxHealth)
{
health = maxHealth;
}
}
float CalculateHealth()
{
return health / maxHealth;
}
public void DealDamage(float damage)
{
health -= damage;
}
}