Animation issue
So I'm trying to learn ow to animate a character for use. After trying to assign "IsJumping" to "Jump" how ever it comes up with this error. "NullReferenceException: Object reference not set to to an instance of an object AnimationController.Update() (at Assets/AnimationController .cs:17)".
Here is the code. As stated in the error the line in question is line 17.
using UnityEngine; using System.Collections;
public class AnimationController : MonoBehaviour {
static Animator animator;
void start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
if(Input.GetButtonDown("Jump"))
{
animator.SetTrigger("IsJumping");
}
}
}
Answer by Zoogyburger · Mar 15, 2016 at 05:38 PM
In your animator are you sure you have that Trigger: "IsJumping" spelled exactly the same?
Also, you can just declare your animator by
Animator animator;
If this doesn't work, notify me and I will tell you how to make a bool instead.
Answer by marrowick · Mar 15, 2016 at 06:45 PM
Yes, every character is exactly the same, I also switched the declaration to what you suggested, neither worked.
Quick try replacing line 10 with:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
Still nothing, also slight change. It didn't render all the code, so the error isn't line 17, it's line 10 on here.
Just tried your code and it works for me. The two things that I can think of that would set off a "NullReferenceException" is either you don't have an Input for "Jump" or your animator isn't assigned in your Inspector. So click on your player. In the Inspector make sure he has an Animator Component and the right animation controller is assigned in that Animator. Next, go to Edit at the top left corner of the window and go to Project Settings, Input. Look at your Inspector and check if the Jump is assigned or is right button etc.
The Animator is assigned. And "Jump" is assigned to "Space". The exact input settings are as below.
Note: Anything left out is blank.
Name: Jump Positive Button: Space Gravity: 1000 Dead: 0.001 Sensitivity: 1000 Type: $$anonymous$$ey or $$anonymous$$ousebutton Axis: X axis Joy Num: Get $$anonymous$$otion from all Joysticks
All of that is just what I got except my positive button is joystick button 3 but I don't think changing the positive button is going to help you. In your animation controller you should have a transition from your regular state to your jump state and on that transition the Conditions should be IsJumping. If this still won't work I'll tell you how to use a bool.
In your animation controller go to Parameters and Create a new bool called Jumping. On the transition that goes from your regular state to your jump state make the Conditions Jumping, true. In your script replace line 10 with the following line:
anim.SetBool("Jumping", true);
So your code is exactly this:
Animator animator;
void start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
if(Input.GetButtonDown("Jump"))
{
animator.SetBool("Jumping", true);
}
}
Your answer
Follow this Question
Related Questions
How do/should I make my character jump? 1 Answer
I am slowly falling down with the jump animation 4 Answers
Jump animation delayed when on a higher platform. 0 Answers
How to play attack animation multiple times with mechanim 0 Answers
AgrumentNullException And NullReferenceException on animating [Video] 0 Answers