- Home /
NullReferenceException: Object reference not set to an instance of an object
i made an animation of my weapon swinging and it was supposed to play whenever i click the mouse button. now it said "NullReferenceException: Object reference not set to an instance of an object MeleeSystem.Update () (at Assets/MeleeSystem.js:11)" ???? i named my weapon "Hammer". this is my code:
#pragma strict
var thedamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var Hammer : Transform;
function Update ()
{ if (Input.GetButtonDown("Fire1"));
{
Hammer.animation.Play("attack");
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", thedamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Are you absolutely sure that 'Hammer' is initialized in the inspector? It must be initialized by dragging and dropping the 'Hammer' object onto the 'Hammer' variable. Are you absolutely sure that the 'Hammer' object has an animation? Any chance that this script is attached to more than one object and you did not initialize 'Hammer' for one of them?
Answer by arun.pandey89 · Apr 16, 2014 at 06:32 AM
It's pretty clear from first sight . First thing first, make your (var Hammer : Transform) "Public" . As the variable is now public , you can see Hammer Variable in your inspector section. Drag and Drop your Hammer ( Corresponding GameObject ) to initialize it. That's it .
Hope it helps
cheers :)
$$anonymous$$ark it as an answer , If it has helped you . Cheers
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Animation not working? 0 Answers
Modifying AI script to follow gameobject 1 Answer
Creating a file and writing into it? :p 1 Answer
how to drag single sprite among multilple sprites on screen ? 1 Answer