The animation (mySprite) does not play in the update function or it’s calls. It only plays inside the start function.
a) mySprite is shown as ‘OTAnimatingSprite’ object in all functions, performed Debug.log(mySprite).
b) It’s memory intensive, but still tried moving
mySprite = this.GetComponent<OTAnimatingSprite>();
inside the other function in which I do
mySprite.Play("runRight");
This does not work.
My Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public OTAnimatingSprite mySprite;
public static bool isLeft;
public static bool isRight;
public static bool isUp;
public static bool isDown;
/* ============================== CONTROLS ============================== */
// Use this for initialization
void Start ()
{
mySprite = this.GetComponent<OTAnimatingSprite>();
Debug.Log("Player Script Started: " + mySprite);
mySprite.Play("runRight");
}
private void updatePlayerMovement() {
//moveRight();
//moveLeft();
//moveUp();
this.moveRight();
}
private void moveUp() {
this.transform.Translate(Vector3.up * Time.deltaTime*30, Space.World);
}
private void moveDown() {
this.transform.Translate(Vector3.down * Time.deltaTime*30, Space.World);
}
private void moveLeft() {
this.transform.Translate(Vector3.left * Time.deltaTime*30, Space.World);
}
private void moveRight() {
this.transform.Translate(Vector3.right * Time.deltaTime*30, Space.World);
}
public void Update () {
// these are false unless one of keys is pressed
isLeft = false;
isRight = false;
isUp = false;
isDown = false;
// keyboard input
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
isLeft = true;
Debug.Log("Left: " + mySprite);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
isRight = true;
Debug.Log("Right!");
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
isUp = true;
Debug.Log("Up");
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
isDown = true;
Debug.Log("Down!");
}
updatePlayerMovement();
}
}