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!
(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!)