Getter/Setter not working

Good day. I am using Unity 2021.3.22f1. The Get/Set is not being assigned for the Sword animation. I am unsure if i am assigning the value to the FacingLeft bool properly.

public bool FacingLeft { get { return facingLeft; } set { facingLeft = value; } }

[SerializeField] private float moveSpeed = 1f;
private PlayerControls playerControls;
private Vector2 movement;
private Rigidbody2D rb;
private Animator myAnimator;
private SpriteRenderer mySpriteRenderer;

private bool facingLeft = false;

private void Awake()
{
    playerControls = new PlayerControls();
    rb = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    mySpriteRenderer = GetComponent<SpriteRenderer>();
}

private void OnEnable()
{
    playerControls.Enable();
}

private void OnDisable()
{
    playerControls.Disable();
}

private void Update()
{
    PlayerInput();
}
private void FixedUpdate()
{
    Move();
    FlipPlayerPosition();
}

private void PlayerInput()
{
    movement = playerControls.Movement.Move.ReadValue<Vector2>();
    myAnimator.SetFloat("moveX", movement.x);
    myAnimator.SetFloat("moveY", movement.y);

}

private void Move()
{
    rb.MovePosition(rb.position + movement * (moveSpeed * Time.fixedDeltaTime));
}

private void FlipPlayerPosition()
{
    Vector3 mousePos = Input.mousePosition;
    Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(transform.position);
  
    if (mousePos.x < playerScreenPos.x)
    {
        mySpriteRenderer.flipX = true;
        FacingLeft = true;
    }
    else
        mySpriteRenderer.flipX = false;
        FacingLeft = false;
}

}

The FacingLeft property looks good and should work. The only thing I would do is to make the Setter private (because this script should be deciding if we’re facing left or not, and otherwise should be a read only propery.

Can you show the code that reads the FacingLeft property?

private GameObject slashAnim;

 public void SwingUpFlipAnim()
    {
        slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180, 0, 0);
        if(playerController.FacingLeft)
        {
            slashAnim.GetComponent<SpriteRenderer>().flipX = true;
            Debug.Log("Left Up Slash");
        }
    }

    public void SwingDownFlipAnim()
    {
        slashAnim.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);

        if (playerController.FacingLeft)
        {
            slashAnim.GetComponent<SpriteRenderer>().flipX = true;
            Debug.Log("Left Down Slash");
        }
    }

Is the rest of the logic working? Is the slash animation swinging up and down properly (aside from the left or right, I mean).

Yes, everything else works. The facing left slash will work if I change “value” to “true” in the get:set statement. I feel like I may have something in the logic of the statement mistyped in some way but it is evading me

I found the error. I was missing brackets {} on the “else” statement. Put those and now it works.

I see it now too. Often it’s these little details that trip us up.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms