I’m making a 2D movement script with JS and can’t really figure out what I’m doing wrong. I keep getting
“NullReferenceException: Object reference not set to an instance of an object”
on different lines. I think I managed to fix it for var anim : Animator;, but now it’s on line 55. (Which for some reason is line 57 here.)
This is my script so far:
` var Speed : float = 80; //Max Vertical Movement Speed var FacingRight : boolean = false; // Is Character Facing Right? var FacingLeft : boolean = false; // Is Character Facing Left? var JumpForce : float = 20; // Jump Speed var anim : Animator; // The animator var Grounded : boolean = false; static var GroundRadius : float = 0.2; static var WhatIsGround : LayerMask; static var GroundCheck : Transform; function Start(){ anim = gameObject.GetComponent(Animator); } function parametercheck(){ anim.SetBool("FacingRight",FacingRight); anim.SetBool("FacingLeft",FacingLeft); } function HorizontalMove(){ var move : float = Input.GetAxis("Horizontal"); anim.SetFloat("XSpeed",move); var XAxisSpeed : float = anim.GetFloat("XSpeed"); var charscale : Vector3 = transform.localScale; anim = GetComponent(Animator); rigidbody2D.velocity = Vector2(move * Speed, rigidbody2D.velocity.y); if(XAxisSpeed == 0){ parametercheck(); FacingRight = false; FacingLeft = false; } if(XAxisSpeed > 0){ parametercheck(); FacingRight = true; FacingLeft = false; } if(XAxisSpeed *Less than Symbol* 0){ parametercheck(); FacingRight = false; FacingLeft = true; } } function VerticalMove(){ anim.SetFloat("YSpeed", rigidbody2D.velocity.y); if(Grounded && Input.GetKeyDown(KeyCode.Space)){ anim.SetBool("OnGround", false); rigidbody2D.AddForce(new Vector2(0, rigidbody2D.velocity.y)); } } function FixedUpdate(){ Grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, WhatIsGround); anim.SetBool("Ground", Grounded); HorizontalMove(); VerticalMove(); // Jumping and falling } `
There’s probably some other things I’m doing wrong too…
Edit: The less than symbol part should have that there, but the bbcode messed it up.