Enemy Health Won't go Down
I'm following a tutorial on youtube (https://www.youtube.com/watch?v=Q_o74dbn3ZU&list=PLZ1b66Z1KFKik2g8D4wrmYj4yein4rCk8∈dex=11). They are using unity 5 and I'm using unity 5.6. Our scripts are identical and attached to the same things, but I have the error "NullReferenceException: Object reference not set to an instance of an object AttackScript.Update () (at Assets/Scripts/AttackScript.js:11)". This is the script: var hitpoint : int = 10; var totarget : float; var range : float = 5;
function Update () { if (Input.GetButtonDown("Attack")) { var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) { totarget = hit.distance; if (totarget < range) { hit.transform.SendMessage("DeductPoints", hitpoints, SendMessageOptions.DontRequireReciever); } } } }
What did I do wrong?
Answer by $$anonymous$$ · Jun 09, 2017 at 09:23 AM
@Joebear Looks like you declared a variable called hitpoint at the beginning but then reference hitpoints with an s in your sendmessage function.
I fixed that, but I still have the same problem. The error only comes up when I strike the enemy.
@Joebear Your code looks almost identical. I usually would get that error if a GameObject did not exist and I was trying to call a function on it.
I did notice that you have Send$$anonymous$$essageOptions.DontRequireReceiver spelled differently.
$$anonymous$$aybe that could be it.
Oh my god that's it. I don't know why I have such a tendency to make dumb mistakes like that. Thanks so much.
Your answer