Hello, I think I’m missing something since I started to believe I have a bug 
I have two scripts, “enemy” and “playercontroller”. When my enemy shoots, I want to decrease the health points of my character.
For this purpose, I have defined a float as playerHealth and set it to 100 in Start function and I have created the following function to access it on “enemy” script. So far, I have defined everything and double checked the values.
public void HealthStatus(float damage)
{
playerHealth -= damage;
Debug.Log(playerHealth);
}
Then in my enemy script, I use RaycastHit to target my player and deal damage over fire rate. In this part, I try to access the HealthStatus function from playercontroller with GetComponent. I have public hitDamage defined as float which is set to 10.
RaycastHit hit;
if (Physics.Raycast(firePoint.transform.position, firePoint.transform.forward, out hit, range))
{
if (Time.time > fireRateBase)
{
hit.transform.gameObject.GetComponent<charactercontroller>().HealthStatus(hitDamage);
Instantiate(fireEffect, firePoint.transform.position, Quaternion.identity);
fireRateBase = Time.time + fireRate;
}
}
I have defined everything yet I get NullReferenceException. I’m still learning Unity and I guess I’m missing something. Anyone has any idea? Thanks in advance.
I get the error for
hit.transform.gameObject.GetComponent<charactercontroller>().HealthStatus(hitDamage);
Also, I can find a solution with
Player.GetComponent<charactercontroller>().HealthStatus(hitDamage);
but just wondering the mistake I do here with hit.transform.
Apparently not!
Any ideas? No ideas needed… the answer is ALWAYS the same.
It’s such a common error we have a pinned post for it.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Steps to success:
- Identify what is null
- Identify why it is null
- Fix that
Yes, I checked the pinned post before typing here. I checked every line and every reference before posting too. Later, I found a solution by changing my approach and using the GameObject but I still cannot figure out the exact reason why I’m getting the error.
The reason I asked for help is to learn what I’m missing. As far as I check nothing is null and I still cannot figure out why it doesn’t work 
One possible (the only logical) reason is the hit does not actually hit my player, which I referred as gameObject. However, it perfectly works when I try other methods to check its validity.
You have made now three posts and as far as I can tell, the closest your posts have come to performing Step 1 - Identify What is Null has been your second post, where you posted a hairy line of code.
Hairy lines of code are expressly detailed as a problem in the pinned post because they PREVENT you from doing Step #1.
Until you break that FIVE STEP line into separate statements, you’re going to have to keep guessing in the dark. Once you break it down you will INSTANTLY know.
If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
How to break down hairy lines of code:
http://plbm.com/?p=248
Break it up, practice social distancing in your code, one thing per line please.
“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums
1 Like
That seems like a good idea
I got it better now about how to figure out most of my problems on my own now.
Thanks a lot.
2 Likes