- Home /
Problem is not reproducible or outdated
i don't understand "NullReferenceException: Object reference not set to an instance of an objec"
Hello so i try to understand by searching "NullReferenceException: Object reference not set to an instance of an object" on google but i didn't find my answer so everytime i click on play this message is spamming the console, I'm trying to put animations of a character in a script (Idle, Run, Walk,...)
there is my Script:
//Public variables
var speed:float;
var speedRun:float;
var speedRotate:float;
var gravity:float;
//Private variables
private var controller:CharacterController;
private var moveDirection:Vector3;
private var deltaTime:float;
private var characterContent;
private var runAnim:boolean;
function Start () {
controller = GetComponent("CharacterController");
characterContent = transform.find("Perso");
}
function Update () {
//Cadence du temps
deltaTime = Time.deltaTime;
//On ne cours pas
runAnim = false;
//Deplacements Haut/bas
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)){
moveDirection = Vector3(0,0,Input.GetAxis("Vertical") * speedRun);
runAnim = true;
}else{
moveDirection = Vector3(0,0,Input.GetAxis("Vertical") * speed);
}
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow)){
if(!runAnim){
characterContent.animation.CrossFade("Walk", 0.2);
}else{
characterContent.animation.CrossFade("Run", 0.2);
}
}else{
characterContent.animation.CrossFade("Idle_01", 0.2);
}
//changer sur l'axe
moveDirection = transform.TransformDirection(moveDirection);
//rotation personnage
transform.Rotate(Vector3(0,Input.GetAxis("Horizontal") * speedRotate * deltaTime,0));
//gravity
moveDirection.y -= gravity;
//Deplacement du Character Controller
controller.Move(moveDirection * deltaTime);
}
So The error seems to be on the line 45 (in MonoDevelop)or line 23 (on this topic) which is " characterContent.animation.CrossFade("Idle_01", 0.2);" Tne animation of the idle is called "Idle_01" on unity so i don't understand where the error came from ... If someone can help me it would be great !
In your start function... is it Perso or Person? Just curious since this may not be found if there are misspellings.
characterContent = transform.find("Perso");
Are you sure the animation "Idle_01" is attached to the animation component of your character?
Answer by Bilelmnasser · Jun 06, 2014 at 09:49 PM
NullReferenceException: Object reference not set to an instance of an object mean you trying to access an object that not been assigned (null) , make sure the animation is attached to your game object character :)
haha i see it now , change this line to this :
characterContent = transform.find("Perso");
//change it to this line
characterContent=gameObject.GetComponent(Perso);
Make sure your script is attached to the parent object Glem :)
Use Debug.Log to print the characterContent and characterContent.animation
One of them is null. Once you find out which, you cans address why it is that way.
@Jeff$$anonymous$$esselman thanks for the answer but i'm new on unity and i try to find by myself but i can't find, i check this page about the debug: http://docs.unity3d.com/ScriptReference/Debug.Log.html
But i don't understand where have i to put the commands and what put exactly ? If someone have a tutorial for me or can explain me in details step by step it's really kind (sorry for my (bad) english)
Even if the answer is obvious please post it because i'm a beginner :)
characterContent = transform.find("Perso");
//change it to this line
characterContent=gameObject.GetComponent(Perso); $$anonymous$$ake sure your script is attached to the parent object Glem :)
Follow this Question
Related Questions
NullReferenceException:"Object reference not set to an instance object" 1 Answer
Script decides not to run anymore 1 Answer
Object reference not set to an instance of an object 0 Answers
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer
Referencing Objects in Unity 1 Answer