- Home /
Animation Scripting c# help
Ive been trying to get this right for ages and ive decided to ask you guys. Does anyone have any idea why its not playing the animation when i assign this script to the parent of the model im trying to animate? using UnityEngine; using System.Collections;
public class EnemyAnimation : MonoBehaviour { Animation _animation;
void Start()
{
_animation = GetComponentInChildren();
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;
}
}
Unity debug screen was saying the line "_animation[animationToPlay].wrapMode = WrapMode.Loop;" was wrong. Sorry if i haven't displayed this correctly Just any help would be much appreciated!
What's the error message? Does it occur at compile time or run time?
(p.s. - When posting, please click the '101/010' button before pasting in code, so it gets formatted properly.)
Not sure if this helps, but shouldn't the default case in your switch statement be the LAST element of the switch?
Default is what the switch will drop to if the other cases aren't met. This code suggests that case 0 is your default. Sometimes logical errors can have knock on effects to other parts of your code.
Thanks for the tip :) During runtime. The message is "NullReferenceException: Object reference not set to an instance of an object EnemyAnimation.Start() (at Assets/PlayerAnimation.cs:23)
Try changing _animation[animToPlay].normalizedTime = 0.0f;
hey guys regarding the above correction i still cant get it to fix, i have the best overload method error do you guys know the reason for it?
Answer by SubatomicHero · Jun 03, 2013 at 01:56 PM
This line is causing the error:
_animation[animationToPlay].wrapMode = WrapMode.Loop;
because you are using a string to access an element of an array, which should be accessed using an integer of somesort.
So in your Start() function add these bits:
// at the top with _animation
int animToPlay;
// then in each case of your switch do this
case 0:
animationToPlay = "Move";
animToPlay = 0; // 1, 2, 3 etc depending on the case
break;
// then at the bottom change the code to:
_animation[animToPlay].wrapMode = WrapMode.Loop
_animation.Play(animationToPlay);
_animation[animToPlay].normalizedTime = Random.value;
Hope this helps.
Hey no problem, can you please mark my answer as correct. Thanks!
Answer by wraithblade · Jul 23, 2015 at 10:55 AM
using UnityEngine; using System.Collections;
public class EnemyBodyScript : MonoBehaviour {
void Start() {
int
_animation = GetComponentInChildren();
string animationToPlay = "";
switch (Random.Range (0, 3)) {
default:
case 0:
animationToPlay = "Move1";
animationToPlay = 0;
break;
case 1:
animationToPlay = "Move2";
animationToPlay = 1;
break;
case 2:
animationToPlay = "Move3";
animationToPlay = 2;
break;
case 3:
animationToPlay = "Move4";
animationToPlay = 3;
break;
case 4:
animationToPlay = "Move5";
animationToPlay = 4;
break;
case 5:
animationToPlay = "Move6";
animationToPlay = 5;
break;
case 6:
animationToPlay = "Move7";
animationToPlay = 6;
break;
}
_animation[animationToPlay].wrapMode = WrapMode.Loop;
_animation.Play(animationToPlay);
_animation[animationToPlay].normalizedTime = Random.value;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trigger GUI with a key when colliding (C#)? 1 Answer
NullReferenceException problem 2 Answers
Pause Game When Function Is Enabled? 3 Answers
Player lives script help 1 Answer