"Operator '+=' cannot be applied to operands of type 'PlayerMovement' and 'float'"

I am referencing a few variables so I may add speed to my players movement once I pick up a coin. When I attempt to do so, I get this error.

error CS0019: Operator ‘+=’ cannot be applied to operands of type ‘PlayerMovement’ and ‘float’

My code:

public class Coin : MonoBehaviour
{
    public PlayerMovement runSpeed;
    public PlayerMovement jumpSpeed;
    public PlayerMovement climbSpeed;
    [SerializeField] AudioClip coinPickupSFX;
    [SerializeField] int pointsForCoinPickup = 100;

    bool wasCollected = false;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Interactables" && !wasCollected);
        {
            wasCollected = true;
            FindObjectOfType<GameSession2>().AddToScore(pointsForCoinPickup);
            AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position);
            gameObject.SetActive(false);
            Destroy(gameObject);
            runSpeed += 1f;
            jumpSpeed += 1f;
            climbSpeed += 1f;
       
        }
    }
}

depends an almighty lot what playermovement is, but unless you overwrote + chances +1f doesnt work would be correct

We need to see the code for PlayerMovement.

As has been pointed out a PlayerMovement object is not a float (it seems). So without some operator overloading it won’t work BUT… I always look at the code surrounding a question.

You could create a method that would increment, decrement or otherwise set the value of a property on the class. Seemingly (but I don’t know what they are doing) the PlayerMovement class could have 3 such properties representing run, ump and climb (and any others that come along). You wouldn’t need to assign 3 separate objects to them and (should it become necessary) you can set all 3 of them with common methods. Think .Reset for instance or Save/Restore.

[SerializeField] public float runSpeed = 10f;
    [SerializeField] public float jumpSpeed = 7f;
    [SerializeField] public float climbSpeed = 5f;

these are the definitions for what I am referencing. There is a lot of code in that script so if you need anything aside from the definitions lmk.

ok so public PlayerMovement runSpeed; is still not a float, nor is it pointing to runspeed on playermovement… you seem very confused

haha how’d u know. I am very confused. I’m having a buddy of mine help, so I think I got it from here on out :slight_smile: