- Home /
Strange index < m_IntCount error using unity2D
I'm creating a 2D game using Unity. I've created a set of animations using a single sprite texture split into multiple sprites and then keyframed using unity's animation editor.
These animations are also controlled by an animation controller attached to the main sprite. In the animator the transition between animations follows the 2D example provided by unity. When the player idle the idle animation will play, when the speed is greater than 0.1 then the running animation will play. Back to idle if less than 0.1.
The script attached to the player sprite is the same player controller from the 2D example without the jump stuff added:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
[HideInInspector]
public bool facingRight =true;
//TODO Add variables for jumping
public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
// TODO Ground checks and Jumping.
}
void FixedUpdate()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Also attached to the sprite is a rigidbody2D and a 2DBox collider.
When I click play to test this however I get the same error: index < m_IntCount over and over. The player sprites behaviour is as follows: he will begin the float upwards slowly, you can sill move him left and right but the run animation will not play.
Where am I going wrong?
I don't see any weird thing in the posted code. Are you able to double click on the error ? $$anonymous$$aybe it shows you the specific line that rises this error.
The error is not able to be clicked on at all, double click it and you get nothing and no further details about it either.
Answer by aniv · Dec 18, 2013 at 09:30 PM
This error happened when I deleted Mecanim variable and created one with the same name, but different type. Try recreating your transitions.
Answer by notreally · Feb 07, 2014 at 07:14 AM
I ran into the same problem. I wish I could give you a better answer but the issue appears to stem from transitions out of "Any State" in the Animator. I started my animations over but did not include transitions out of Any State and did not run into any issues.
Answer by taxvi · Feb 17, 2014 at 07:25 PM
I ran into same problem when I renamed a variable in mecanim and changed its type
just delete all the transitions that are using that variable and recreate them. it's kinda stupid but it works :))
Answer by HowBoring · Aug 26, 2014 at 11:30 AM
I got the same issue and now I've solved it. The reason of my problem was that TransitionConditionMode was wrong. I should've set the mode as 'If', but I set it as 'Equal'.
Your answer
Follow this Question
Related Questions
Character Animator controller/movement 1 Answer
How can I create a home position where my character returns to after every animation? 0 Answers
How can I change my AnimatorState instantly? 1 Answer
Animation Mecanim - Parts of model displaces after animating 0 Answers
Animator.CrossFade or Animator.CrossFadeInInFixedTime does not play animation after crossfading. 0 Answers