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
Answer by sora_jp · Mar 04, 2017 at 08:31 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612112445im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
unable to attack enemy objects? 1 Answer
Rayscast from the GameObject that is not the player does not work 0 Answers
Error `UnityEngine.Component' does not contain a definition 1 Answer
It does not work removing items from the inventory 0 Answers
UI Text: Words at end of line jumping to next line 0 Answers