- Home /
Unassigned variable is assigned
I am having a strange bug. I have a couple public GameObject variables, and I have set them in the inspector. the enemy variable is fine, but the left_fist variable keeps giving me this error message.
UnassignedReferenceException: The variable left_fist of 'Player AI' has not been assigned.
You probably need to assign the left_fist variable of the Player AI script in the inspector.
Player AI.forward_punch () (at Assets/Player AI.js:29)
Player AI.Update () (at Assets/Player AI.js:24)
The strange thing is the function still executes, the fist moves. It doesn't come back, that's a separate problem, but it goes out. Does anyone know why I am getting the error?
var enemy : GameObject;
var left_fist: GameObject;
var punching = false;
var punching_forward = false;
function Update () {
var distance = Vector3.Distance(enemy.transform.position, transform.position);
if(enemy.renderer.material.color == Color.red){
gameObject.renderer.material.color = Color.blue;
} else if(enemy.renderer.material.color == Color.blue){
gameObject.renderer.material.color = Color.red;
} else{
gameObject.renderer.material.color = Color.green;
}
if(Input.GetKeyDown("space")){
punching = true;
punching_forward = true;
}
forward_punch();
}
function forward_punch(){
if(punching){
var current_fist_position = left_fist.transform.position;
var distance_from_body = Vector3.Distance(transform.position, current_fist_position);
if(punching_forward){
if(distance_from_body < 5){
var new_position = (current_fist_position + transform.forward);
left_fist.transform.Translate(new_position * Time.deltaTime);
} else {
punching_forward = false;
}
Debug.Log(punching_forward);
Debug.Log(distance_from_body);
Debug.Log("");
Debug.Log("");
} else{
if(distance_from_body > .5){
new_position = (current_fist_position - transform.forward);
left_fist.transform.Translate(new_position * Time.deltaTime);
} else
punching = false;
}
}
}
What do you have in the left_fist Inspector slot? When you press Play, does whatever was there go away for some reason?
I have a game object there. It's just a cube. It's there I can see it and I can mess with it. The code works for the most part I just keep getting that error message.
I got the same error message in one of my games. it said that a transform wasn't set that I had set in the inspector. Ultimately I ended up ignoring it since the game ran just fine both in editor and in build. I think the editor is just confused, but if there's something else I'd love to know why it's doing it.
If you are absolutely sure that the 'left_fist' exists at both while the app is not playing and during play, then the most likely issue is that you have this script attached to more than one object. In the upper right of the Hierarchy, there is a search field. Type the script name into that field to see if you have other objects with this script.
@robertbu you got, I accidentally put it on the fist too. $$anonymous$$ove your comment to the answer and get your vote.
Your answer
Follow this Question
Related Questions
unassignedreferenceexception - script used in multiple scenes 0 Answers
UnassignedReferenceException: The variable enemy of EnemiesSpawner has not been assigned unity3d 0 Answers
cashing in Scenes to a public vaiable 1 Answer
Shift-Click on Scene View to add Vector2 0 Answers
Is it possible to define a public variable of type UnityEngine.Object? 0 Answers