- Home /
Animation Scripting C# Error
I have already asked this question, but I really need a through walk through on how to fix it, I would really appreciate this because I am doing self teaching but I cannot figure this error out...
The Error is :
NullReferenceException: Object reference not set to an instance of an object EnemyAnimation.Start () (at Assets/The Game/Scripts/EnemyAnimation.cs:24)
using UnityEngine;
using System.Collections;
public class EnemyAnimation : MonoBehaviour
{
public Animation _animation;
// Use this for initialization
void Start ()
{
_animation = GetComponentInChildren<Animation> ();
string animationToPlay = "";
switch (Random.Range (0, 3)) {
default:
case 0:
animationToPlay = "Move1";
break;
case 1:
animationToPlay = "Move2";
break;
case 2:
animationToPlay = "Move3";
break;
}
_animation [animationToPlay].wrapMode = WrapMode.Loop;
_animation.Play (animationToPlay);
_animation[animationToPlay].normalizedTime = Random.value;
}
// Update is called once per frame
void Update ()
{
}
}
Here are some pictures on how I added it to the component on unity. Hopefully it is visible.
Also where it says Animation in the script component, it wont let me add anything, when I go to add something it gives me the options and when I select an animation, it does not update...
Answer by ohboyotero · Apr 18, 2014 at 05:07 AM
If the line numbers in your posted code match the error, that would suggest that either:
"_animation" is null (meaning that the GetComponentInChildren() call above returned null), or
"_animation[animationToPlay]" is null.
As far as why these are null, that depends on what you're trying to do. Can you explain what you're trying to do with this code? Why are you calling GetComponentInChildren() to find the animation, for instance?
It is so it can play an animation for the zombie I have.
and I am randomizing between 3 animations so that all the zombies are not doing the same animations.
Here is what I am doing:
http://dl.dropboxusercontent.com/u/25260770/UnityLessons/FeuUnity3dLesson9.pdf
It is on page 10.
Please help D:
Two things:
Could you add a few lines of code right after "_animation = GetComponentInChildren<Animation>();" on line 10?
Debug.Log("_animation is: " + _animation);
Debug.Log("_animation[$$anonymous$$ove1] is: " + _animation["$$anonymous$$ove1"]);
Debug.Log("_animation[$$anonymous$$ove2] is: " + _animation["$$anonymous$$ove2"]);
Debug.Log("_animation[$$anonymous$$ove3] is: " + _animation["$$anonymous$$ove3"]);
Run the program and look at the console and figure out if any of those lines print "blah blah is: null." That'll tell you what your problems is.
The code you're following doesn't make "animation" public (in fact, usually the underscore prefix indicates "private member variable"), so you shouldn't be trying to set it from the editor like you're suggesting. You're just going to overwrite it when you call:
_animation = GetComponentInChildren<Animation>();
Your answer
Follow this Question
Related Questions
Animation spins wildly after completed 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation for weapon plays on game start instead of when i attack 2 Answers
Dataset is null 1 Answer
Animation scripting problems 1 Answer