Can't multiply a public float by -1?

Good evening,

I’m currently working on a project in which I am trying to multiply a public float by -1 in another script.

Essentially, I have an enemy movement script which stores the distance to move the player as a public float, called movementamount.
Here’s the code for the enemy movement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
    public float delaybetweenmovement = 1;
    public float movementimer = 0;
    public float movementamount = 0.025f;
    // Update is called once per frame
    void Update()
    {
        movementimer += Time.deltaTime;
        if (movementimer > delaybetweenmovement){
            movementimer = 0;
            transform.position += new Vector3(movementamount,0,0);
        }
    }
}

This all works fine.

The problem is when I attempt to access the public float in another script, it doesn’t work.
I have this code here that is attached to my boundary wall. It is supposed to make the enemy move the opposite direction, but it just throws an error:
Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyDeflect : MonoBehaviour
{
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            collision.gameObject.GetComponent<EnemyMovement>().movementamount * -1;
        }
    }
}

And the error is as follows:

Only assignment, call, increment, decrement, await and new object expressions can be used as a statement.

I’m not sure where I’ve gone wrong here? I’ve tried changing the -1 to -1.0f, but to no avail. This is particularly perplexing because I essentially reused working code from my enemy health script, which works in a nearly identical way. Am I missing something simple? Or is this some kind of Unity-specific limitation? Any help is welcome! :slight_smile:

(By the way, this is one of my first projects, and generally I am new to both Unity and C#. Sorry for my rather primitive way of changing public variables, instead of using public methods with private variables, which is the better practice I have heard. This project is just for school, and it’s a learning experience. Thanks in advance!)

The line there is not a statement, it is an expression. Not all expressions can be statements in C#, so this is a syntax error.

The workaround for this is:

PS: this is my first time answering any of the questions threads.

Like ColossalMonk said, you do not change the variable but only calculate the inverse of the value but you don’t do anything with that value. This is not even valid syntax as the compiler has no idea what it should do with that value. You probably wanted to do

collision.gameObject.GetComponent<EnemyMovement>().movementamount *= -1;

The A *= B operator is a shorthand for A = A * B.
Another option (which is also better to avoid long lines of code) would be this:

var eMovement = collision.gameObject.GetComponent<EnemyMovement>();
eMovement.movementamount = -eMovement.movementamount;

or do

// ...
eMovement.movementamount *= -1;

Since we have the TryGetComponent method, we can even make the whole thing more compact:

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.TryGetComponent(out EnemyMovement eMovement))
        {
            eMovement.movementamount *= -1;
        }
    }

Here we don’t check for a tag but just test if the object we’re colliding with has an EnemyMovement component.