- Home /
Getting the object reference not set to an instance of an object error when raycasting
Hey guys, I am trying to raycast and attack an enemy causing it to lose health. I ran into this error when I am sending the ApplyDamage message.
NullReferenceException: Object reference not set to an instance of an object
MeleeSystem.Update () (at Assets/Scripts/MeleeSystem.cs:22)
This is the line that the error is thrown on
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Here is my attack script code
public class MeleeSystem : MonoBehaviour {
int Damage = 50;
float Distance;
float MaxDistance = 2;
Transform TheSystem;
void Update() {
if (Input.GetKeyDown("l")) {
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
Debug.DrawLine(transform.position, hit.point, Color.cyan);
Debug.Log("Attack Initiated");
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("Sending attack!");
}
}
}
}
Here is the enemy code where the message is recieved
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int health = 100;
void ApplyDamage(int Damage){
Debug.Log ("Damage being applied... Stand by");
if (health > 0){ // if enemy still alive (don't kick a dead dog!)
health -= Damage; // apply the damage...
// <- enemy can emit some sound here with audio.Play();
if (health <= 0){ // if health has gone...
Debug.Log ("You killed it");
}
}
}
}
Please help me fi the error and be able to apply damage to my enemy.
I don't use Send$$anonymous$$essage so not sure what the 'donotrequirereceiver' does, but what if the thing the ray hits is not an enemy? Around line 16 (top code) add
Debug.Log(hit.name);
Verify the ray is hitting what you are expecting it to.
It is throwing an error when I try to add that because ray does not contain name, but what is the way you do yours? I do not have to do raycasting, I just need to be able to kill an enemy
Now only the error shows up. Is there a better way to do this than raycasting? If so, do tell
hit.transform.name does not return anything? That seems unlikely if you added it in the if(Raycast.hit)
There are other ways, but getting a handle on the basic RayCast functions will serve you. If you are desperate for a different approach, check out Physics.OverlapSphere or similar.
Answer by HarshadK · Jan 13, 2015 at 10:27 AM
The problem lies in the placement of braces in the script. Everything you have that works with the Raycast hit should be inside your if(Physics.Raycast....) code block and your current placement of braces was a little off.
Here's the correct one:
void Update ()
{
if (Input.GetKeyDown("l"))
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.forward, out hit))
{
Debug.DrawLine (transform.position, hit.point, Color.cyan);
Debug.Log ("Attack Initiated");
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.Log ("Sending attack!");
}
}
}
}
Answer by Baste · Jan 13, 2015 at 10:32 AM
I copied your MeleeSystem script to my editor, hit "auto format", and then edited it back into your post. Take a look at it and see if you can spot the error!
If not: your brackets are all wrong! In this part of the code:
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
Debug.DrawLine(transform.position, hit.point, Color.cyan);
Debug.Log("Attack Initiated");
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("Sending attack!");
}
The if-check on the raycast only stops the DrawLine, all of the rest of the code runs no matter if the raycast hits or not. The brackets after the "Attack Initiated" message doesn't do anything here.
Just move the bracket after "attack initiated" up to after the if, and you're good to go.
Your answer

Follow this Question
Related Questions
How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers
Detecting if mouseclick is within path 1 Answer
Physics.Boxcast not working with mesh collider 1 Answer
Raycast does not work with touch input but works well with mouse input why? 0 Answers
Tangent Space Normal Map to World Space Normal Vector in C# Script 0 Answers