Play audio code example does not work.

HI there,

I am trying to use the code example for audio playing as from (Unity - Scripting API: AudioSource.Play)

However, the code does not work. It is telling me line 21 of my code, which is the example code:

audioData = GetComponent<AudioSource>();

does not work and gives the errors:
error CS1519: Invalid token ‘=’ in class, struct, or interface member declaration
error CS1519: Invalid token ‘(’ in class, struct, or interface member declaration
error CS8124: Tuple must contain at least two elements.

There has been no change of code except I have not used the variable name audioData and have gone with “snd_Open”

My full code for this script is as follows:

using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class OpenableDoor : MonoBehaviour
{
    AudioSource snd_Open;

    // Smoothly open a door
    public float doorOpenAngle = 90.0f; //Set either positive or negative number to open the door inwards or outwards
    public float openSpeed = 2.0f; //Increasing this value will make the door open faster

    snd_Open = GetComponent<AudioSource>()
    bool open = false;
    bool enter = false;

    float defaultRotationAngle;
    float currentRotationAngle;
    float openTime = 0;

    void Start()
    {
        defaultRotationAngle = transform.localEulerAngles.y;
        currentRotationAngle = transform.localEulerAngles.y;
    }

    // Main function
    void Update()
    {
        if(openTime < 1)
        {
            openTime += Time.deltaTime * openSpeed;
        }
        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Mathf.LerpAngle(currentRotationAngle, defaultRotationAngle + (open ? doorOpenAngle : 0), openTime), transform.localEulerAngles.z);

        if (Input.GetKeyDown(KeyCode.F) && enter)
        {
            open = !open;
            currentRotationAngle = transform.localEulerAngles.y;
            openTime = 0;
            snd_Open.Play(0);
        }
    }

    // Display a simple info message when player is inside the trigger area
    void OnGUI()
    {
        if (enter)
        {
            GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
        }
    }

    // Activate the Main function when Player enter the trigger area
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = true;
        }
    }

    // Deactivate the Main function when Player exit the trigger area
    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = false;
        }
    }
}

Line 12 will cause issues because you cannot do an assignment like that outside of a function unless you are declaring the variable. You cannot do this on line 12: snd_Open = GetComponent<AudioSource>()
(you also need a semicolon)

When you create a variable, you can assign a value at the time you create it. Because null is a value, you could do this on line 6 if you wanted: AudioSource snd_Open = null;

However, you cannot call a function from outside of a function, so you also cannot do this on line 6: AudioSource snd_Open = GetComponent<AudioSource>();

To solve your problem
You will want to move your line 12 to inside of your start function. Here’s what your script should look like with the fix:

using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class OpenableDoor : MonoBehaviour
{
    AudioSource snd_Open;

    // Smoothly open a door
    public float doorOpenAngle = 90.0f; //Set either positive or negative number to open the door inwards or outwards
    public float openSpeed = 2.0f; //Increasing this value will make the door open faster
 
    bool open = false;
    bool enter = false;

    float defaultRotationAngle;
    float currentRotationAngle;
    float openTime = 0;

    void Start()
    {
        snd_Open = GetComponent<AudioSource>();
        defaultRotationAngle = transform.localEulerAngles.y;
        currentRotationAngle = transform.localEulerAngles.y;
    }

    // Main function
    void Update()
    {
        if(openTime < 1)
        {
            openTime += Time.deltaTime * openSpeed;
        }
        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Mathf.LerpAngle(currentRotationAngle, defaultRotationAngle + (open ? doorOpenAngle : 0), openTime), transform.localEulerAngles.z);

        if (Input.GetKeyDown(KeyCode.F) && enter)
        {
            open = !open;
            currentRotationAngle = transform.localEulerAngles.y;
            openTime = 0;
            snd_Open.Play(0);
        }
    }

    // Display a simple info message when player is inside the trigger area
    void OnGUI()
    {
        if (enter)
        {
            GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
        }
    }

    // Activate the Main function when Player enter the trigger area
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = true;
        }
    }

    // Deactivate the Main function when Player exit the trigger area
    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            enter = false;
        }
    }
}

Ok that seems to work well, so no function calling in the class section, only in the subfunctions, got it.

I have another query regarding this. The sound which is played is from one audio source component on the associated mesh.If I wanted a second clip to play when the door was closing, how would i do this in the script? All i see is that snd_Open gets an audio component.

How do i make a second variable for a second audio source on the object?

There are a few different ways to do it, but here’s my recommended way.

If you create a public variable, or a variable marked with [SerializeField], you will be able to click-and-drag things into that variable slot using the unity inspector.

So for example… if you have these variable in your class:

public AudioSource snd_Open;
[SerializeField] AudioSource snd_Closed;

That class will have empty slots in Unity when you select an object with that script. You can click-and-drag scripts of the correct type into those empty slots.
4812437--460766--C1.PNG
4812437--460769--C2.PNG

After you do that and save the scene, your two variables “snd_Open” and “snd_Close” will already be set correctly and you do not have to assign their values using GetComponent<AudioSource>(). They’re ready to go, so just simply call “Play()” on them when you want to.

1 Like