- Home /
variable has not been assigned?
Hi, I'm pretty new to Unity, and I'm trying to design my own video game for a school project. The project has been going well so far, but i keep getting this error, something like... "The variable 'whatever' of 'myscript' has not been assigned, you probably need to assign it in the inspector". But i have put my variable in, is it because its a transform? Oh and this happened when i added my two animation codes, it keeps on spamming me with the same error (but the script still works, and i'm able to play the game perfectly). -my code
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var WeaponPipe : Transform;
function Update ()
{
//click
if (Input.GetButtonDown("Fire1"))
{
//animation Attack
WeaponPipe.animation.Play("Attack");
//function 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);
}
}
}
if (WeaponPipe.animation.isPlaying == false)
{
WeaponPipe.animation.CrossFade("Idle");
}
}
-credit goes to the Brackeys tutorial for helping me with the script
So you have dragged a transform object onto WeaponPipe in the inspector? if not then you should do that or use a find function in Start (or elsewhere) to set WeaponPipe.
Answer by felixpk · May 10, 2014 at 04:49 PM
If you use public variables you must:
if it is a Transform or whatever:
Assign a Transform or whatever in the Inspector
or find a reference like:
void Start () { objRef = GameObject.FindGameObjectsWithTag("Player"); }
if it is an Integer or float or so
you can change the Value in the inspector
or declare it like:
public float x = 20F;
if these are private Variables:
make sure you initialized them either when you declared them like above
or you inizialise them in Start(), Update() ect.
Hope that clears your problem.
Felix
Thanks, I hope this works, I'll be trying it out on Tuesday. But I have already put it into the inspector which really confuses me as to why I keep getting the same error.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
What is it "Input Button 87 is not Setup"? 2 Answers
How to change a variable in the same script? 3 Answers
Get information from variable 1 Answer
What is happening and what does this MEAN!?!?!?!?!?!?!?!? AHHHHH!!!!,WHAT IS HAPPENING?!?! 1 Answer