- Home /
NullRefernceExcepception with Raycasting
Run-Time Error:
NullReferenceException: Object reference not set to an instance of an object
Shooting Script.ForceFire () (at Assets/Scripts/Shooting Script.js:170)
Shooting Script.Update () (at Assets/Scripts/Shooting Script.js:108)
Function Causing Error:
function ForceFire () {
audio.PlayOneShot(shotSound); // play the shot sound
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
if (Physics.Raycast(ray, hit, 100)){
var particleClone = Instantiate(bloodPrefab, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
}
}
Complete Code: http://pastebin.com/BjicFJji
Answer by Polantaris · Oct 21, 2013 at 01:41 AM
You have to ensure that the raycast has actually hit something before you use it.
RaycastHit objects will still be created, regardless of if it actually resulted in a collision. As a result, you must check to see if it's actually referencing anything before you begin to do anything with it.
function ForceFire () {
audio.PlayOneShot(shotSound); // play the shot sound
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
if (Physics.Raycast(ray, hit, 100)){
var particleClone = Instantiate(bloodPrefab, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
if(hit.transform) {
hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
}
}
}
If the Raycast failed (IE it returned false), then hit.transform would be null. That's why you're getting a NullReferenceException. hit exists, because you set it to the output of the Raycast, however hit.transform does not exist, because the Raycast failed and hit nothing.
I've applied your code but it still has the same error as before. Code for what is receiving Raycast if it helps: http://pastebin.com/FCJd1Zfh
Which line is Line 170 on your original NullPointerException?
Shooting Script.ForceFire () (at Assets/Scripts/Shooting Script.js:170) Shooting Script.Update () (at Assets/Scripts/Shooting Script.js:108) This basically says that it encountered an error on Line 170, while executing the method "ForceFire" which was executed by "Update" on line 108. This means that Line 170 is the place where the exception occurred. If 170 is the line before the if(hit.transform), then it's because of another reference to the RaycastHit (hit.point or hit.normal). I've never used RaycastHit.point nor RaycastHit.normal before, they may be null for some reason as well. I would add another check on hit.point, as well as hit.normal, to make sure that they are not null before you attempt to use them. Also, are you sure that the other things you are referencing are not null? For example, has bloodPrefab been properly defined as well? Any of those references could result in a NullReferenceException if not properly defined. If you tell us which line is 170, it will be easier to deter$$anonymous$$e which possible references are at fault here.NullReferenceException: Object reference not set to an instance of an object