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

Here’s the code:

using System.Collections;
using UnityEngine;

public class SoundSC : MonoBehaviour {

    public float sensitivity = 100;
    public float loudness = 0;
    AudioSource _audio;
    // Use this for initialization
    void Start() {
        _audio = GetComponent<AudioSource>();
        _audio.clip = Microphone.Start(null, true, 10, 44100);
        _audio.loop = true;
        _audio.mute = true;
        while (!(Microphone.GetPosition(null) > 0) )
        _audio.Play();
		
	}

    // Update is called once per frame
    void Update()
    {
        loudness = GetAveragedVolume() * sensitivity;
        if (loudness > 8)
            this.GetComponent<Rigidbody2D>().velocity = new Vector2(this.GetComponent<Rigidbody2D>().velocity.x, 4);
    }
    //
    float GetAveragedVolume()
    {
        float[] data = new float[256];
        float a = 0;
        _audio.GetOutputData(data, 0);
        foreach(float s in data)
        {
            a + Mathf.Abs(s);
        }
        return a / 256;
    }
}

I don’t know what’s wrong, I think the error is in the this.Getcomponent… = new Vector2(… And the error says “Assets/Hear Me Bounce/Scripts/SoundSC.cs(35,13): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.” What’s the matter?

The error is on line 35;

a + Mathf.Abs(s);

I’m gunna guess you want;

a += Mathf.Abs(s);

Privacy & Terms