UnassignedReferenceException: The variable TheKnife of MeleeSystem has not been assigned.
This is the full error and i need help! UnassignedReferenceException: The variable TheKnife of MeleeSystem has not been assigned. You probably need to assign the TheKnife variable of the MeleeSystem script in the inspector. UnityEngine.Component.GetComponent[Animation] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineComponentBindings.gen.cs:48) MeleeSystem.Update () (at Assets/MeleeSystem.js:12)
This is my script
pragma strict
var TheDamage : int = 50; var Distance : float; var MaxDistance : float = 1.5; var TheKnife : Transform;
function Update () { if (Input.GetButtonDown("Fire1")) { TheKnife.GetComponent.().Play("Attack"); var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) { Distance = hit.distance; if (Distance < MaxDistance) { hit.transform.SendMessage("ApplyDammage", TheDamage, SendMessageOptions.DontRequireReceiver); } } } }
What is the problem with this?
Answer by EAssassinator · Jan 23, 2016 at 10:42 PM
From what that error message is saying, you forgot to assign your knife object to the TheKnife variable in the inspector. To fix it click on the object that has the script on it find your script and drag your knife model onto the TheKnife variable.
Just for future reference please format your scripts. I can assure you that if you format your scripts it'll be much easier to understand and find problems with your script. :)
Answer by SterlingSoftworks · Jan 23, 2016 at 10:36 PM
The error is saying exactly what your problem is. Your "TheKnife" variable is not assigned. It's declared, but it's simply an empty Transform variable in your current code. The computer, or rather, Unity, doesn't know what you mean by "TheKnife". All it knows is that there's a Transform type variable with the name "TheKnife". You need to assign the "TheKnife" variable to what you want it to be.
In your "Start()" method, set up something like
TheKnife = GameObject.FindGameObjectWithTag("enterknifetaghere").transform;
This now assigns your "TheKnife" variable to the transform you are trying to manipulate, or at least, that's what it looks like you're trying to do from what you've given us.