- Home /
Unity will not run when I assign an animation to a keystroke
Hi all,
I am running Unity 4.3.1f1 on a 2013 MacBookPro with OS X 10.9.3.
So, I am trying to create an animation using scripting.
This is my current code: using UnityEngine; using System.Collections;
public class HorseAnim : MonoBehaviour {
public Sprite[] spritesRight;
public Sprite[] spritesIdle;
public float framesPerSecond;
private SpriteRenderer spriteRendererRight;
// Use this for initialization
void Start () {
spriteRendererRight = renderer as SpriteRenderer;
}
// Update is called once per frame
void Update () {
int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
index = index % spritesRight.Length;
spriteRendererRight.sprite = spritesRight[ index ];
}
}
When I run Unity using this code, my actor defaults to the animation within no matter what state it is in. When I try to add a keystroke like this:
using UnityEngine;
using System.Collections;
public class HorseAnim : MonoBehaviour {
public Sprite[] spritesRight;
public Sprite[] spritesIdle;
public float framesPerSecond;
private SpriteRenderer spriteRendererRight;
// Use this for initialization
void Start () {
spriteRendererRight = renderer as SpriteRenderer;
}
// Update is called once per frame
void Update () {
while(Input.GetButtonDown("Horizontal"))
{
int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
index = index % spritesRight.Length;
spriteRendererRight.sprite = spritesRight[ index ];
}
}
}
and then preview the scene in Unity, Unity will preview it for a second, but then it will stall, and the spinning pinwheel of doom will appear. I cannot do anything during this period, the game will not run, and I eventually have to force-quit Unity.
Why is Unity crashing when I add that keystroke?
When I try using "if", the actor changes its image only once when I press the button, and I have to release it and press it again for the actor to move to the next frame.
When I use "if", my character only changes its frame by letting go of the key and pressing it again.