Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Getting Started' started by ratsarevalid, May 9, 2024.

  1. ratsarevalid

    ratsarevalid

    Joined:
    Feb 9, 2024
    Posts:
    4
    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:
    Code (CSharp):
    1. public class Coin : MonoBehaviour
    2. {
    3.     public PlayerMovement runSpeed;
    4.     public PlayerMovement jumpSpeed;
    5.     public PlayerMovement climbSpeed;
    6.     [SerializeField] AudioClip coinPickupSFX;
    7.     [SerializeField] int pointsForCoinPickup = 100;
    8.  
    9.     bool wasCollected = false;
    10.  
    11.     void OnTriggerEnter2D(Collider2D other)
    12.     {
    13.         if (other.tag == "Interactables" && !wasCollected);
    14.         {
    15.             wasCollected = true;
    16.             FindObjectOfType<GameSession2>().AddToScore(pointsForCoinPickup);
    17.             AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position);
    18.             gameObject.SetActive(false);
    19.             Destroy(gameObject);
    20.             runSpeed += 1f;
    21.             jumpSpeed += 1f;
    22.             climbSpeed += 1f;
    23.        
    24.         }
    25.     }
    26. }
    27.  
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,165
    depends an almighty lot what playermovement is, but unless you overwrote + chances +1f doesnt work would be correct
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,672
    We need to see the code for
    PlayerMovement
    .
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    692
    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.
     
  5. ratsarevalid

    ratsarevalid

    Joined:
    Feb 9, 2024
    Posts:
    4
    Code (CSharp):
    1. [SerializeField] public float runSpeed = 10f;
    2.     [SerializeField] public float jumpSpeed = 7f;
    3.     [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.
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,165
    ok so public PlayerMovement runSpeed; is still not a float, nor is it pointing to runspeed on playermovement.. you seem very confused
     
  7. ratsarevalid

    ratsarevalid

    Joined:
    Feb 9, 2024
    Posts:
    4
    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 :)