2D animation flipping script Errors

Hello everyone!

I am new comer when it comes to Unity as I have only spent a few hours playing with it an following tutorials, so go easy on me if you can.

I’m using Unity 2D and I’m trying to flip my sprite so when it walks left the sprite flips left and does the walking animation rather than just walk left and it looks like it’s doing a moon walk (As cool as it sounds)… But I keep running into the same 4 errors every time :

  • “Error CS1612: Cannot modify a value type return value of ‘UnityEngine.Transform.localScale’ Consider Storing the value in a temporary variable” (I get this twice for both if statements)
  • “Cannont implicitly convert type ‘UnityEngine.Vector3’ to ‘float’” (Again, from both if statements)

My script is below:

using UnityEngine;
using System.Collections;

public class MoveLeftRight : MonoBehaviour {

	public float speed = 1.0f;
	public string axisName = "Horizontal";
	public Animator anim;


	// Use this for initialization
	void Start () {
		anim = gameObject.GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
		anim.SetFloat ("Speed", Mathf.Abs(Input.GetAxis(axisName))); //sets the speed float condition

		if (Input.GetAxis (axisName) < 0) {

			Vector3 newScale = transform.localScale;
			newScale.x = -1.0f;
			transform.localScale.x = newScale;

		} 
		else if(Input.GetAxis (axisName) > 0)
		{
			Vector3 newScale = transform.localScale;
			newScale.x = 1.0f;
			transform.localScale.x = newScale ;

		}
		transform.position += transform.right*Input.GetAxis(axisName) * speed * Time.deltaTime; //Allows the player to walk on the Horizontal Axis
	
	}
}

Even though this is what the tutorial says I should do it doesn’t work for me at all and I get thrown these errors every time even though for him it works. Again sorry if this is a silly little mistake… We all have to learn somehow :slight_smile:

Make the following change here (and the same thing in the else if block):

       if (Input.GetAxis (axisName) < 0) {
          Vector3 newScale = transform.localScale;
          newScale.x = -1.0f;
        //transform.localScale.x = newScale
          transform.localScale = newScale;
       }