I’ll start out by saying I’m very new to the game dev world. So Im trying to get a simple first person sword swinging animation to play when I press left mouse button in unity 5. Im getting an error that says "UnnasignedReferenceException: The variable sword of melee has not been assigned. Really unsure of what to fix and was wondering if anyone could set me on the right track? Thanks in advance!
#pragma strict
var Damage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var sword : Transform;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
sword.GetComponent.<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", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Reference type variables (as opposed to value types like int, float, bool, struct,
and so forth, which hold data directly) require you to assign them a reference before they can be used, whether this reference stems from an existing object that you want to manipulate (such as a gameobject in the scene), or a new
object that you’re creating then and there (such as a new List
, for example.) You should be able to hover over any type in your IDE and it’ll tell you whether it’s a class
or a struct
; classes are always reference types, whereas structs are always data types.
Transform
is a reference type and your sword
variable references nothing, which is problematic when you use GetComponent
on it–it doesn’t know what GameObject
you’re talking about to get a component from, because it doesn’t know what Transform
you mean which is attached to that GameObject
, thus it throws the UnassignedReferenceException
error: as the name states, your reference is unassigned. So let’s assign it:
First, write a function named Start()
; this is built-in like Update()
, and it gets called once when the script component is first activated. Inside this Start()
function you’ve added, write sword = GameObject.Find("x").GetComponent.()
to assign a Transform
to the sword
variable, where “x” is the name of the gameobject in your scene holding the Transform
you want to reference. Now the compiler knows what Transform
you’re talking about when you use the sword
variable, and now it can grab the Animation
component because it knows what gameobject you’re referencing.
Also, declare a variable of type Animation
and in the Start()
function assign that to sword.GetComponent.()
, so that you aren’t grabbing it every single time you hit the mouse button; it’s faster, tidier and more readable to put all your GetComponent
calls in your Start()
function and to keep these components in separate variables.
function Start(){
anim = GetComponent<Animator>();
//I don't know for sure how to write in here because iam C# user I know something about java but this shoud get you started.
}
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
//make an animator put your animations in there - anim = Animator now every time your mouse button is down animation will play every time button is up it will stop :D
anim.SetBool("Attack",true);
}else{
anim.SetBool("Attack",false);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}