Error CS0119 when attempting to enable/disable Audio Listener

Hello.

I’m having difficulty getting the Audio Listener component to enable/disable in correspondence with the camera switch. Error CS0119: Expression denotes a type, where a variable, value or method group was expected.

Here’s my code.

using UnityEngine;

public class CameraSwitch : MonoBehaviour
{
	public GameObject camera1;
	public GameObject camera2;

	void Start()
	{
		camera1.SetActive(true);
		camera1.GetComponent(AudioListener).enabled = true;
		camera2.SetActive(false);
		camera2.GetComponent(AudioListener).enabled = false;
	}

	void Update()
	{
		if (Input.GetButtonDown("ButtonA") || Input.GetKeyDown("1"))
		{
			camera1.SetActive(true);
			camera1.GetComponent(AudioListener).enabled = true;
			camera2.SetActive(false);
			camera2.GetComponent(AudioListener).enabled = false;
		}
		if (Input.GetButtonDown("ButtonY") || Input.GetKeyDown("2"))
		{
			camera1.SetActive(false);
			camera1.GetComponent(AudioListener).enabled = false;
			camera2.SetActive(true);
			camera1.GetComponent(AudioListener).enabled = true;
		}
	}
}

It seems pretty straightforward, which is why I can’t understand why this error is occurring. Many thanks for the assist!

Jonathan

When using GetComponent in C#, you have to put the type in angle brackets in front of the normal brackets. Replace GetComponent(AudioListener); with GetComponent<AudioListener>();

This is because c# uses generics with angle brackets, and a class or struct name can not be an argument to a function, unlike javascript.

Cheers, Oskar