x


Converting from js to c#...

I'm converting from js to c# and hit this error:

error CS0176: Static member `UnityEngine.AudioListener.volume' cannot be accessed with an instance reference, qualify it with a type name instead

Here's the code:

audioListener.volume = 0.0f; // This is the line returning the error above.

audioListener is populated in Awake(), here:

audioListener = GameObject.FindGameObjectWithTag("MainCamera").transform.GetComponent<AudioListener>();

audioListener is defined as:

public static AudioListener audioListener;

What do I need to do differently?

more ▼

asked May 10 '12 at 09:55 AM

JSorrentino gravatar image

JSorrentino
3 2 2 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In this case, it's because

AudioListener.volume

is itself a static variable! As such, you can't access it from an instance, because it should be accessed only as a member of the class itself. In this case, whenever you want to change the volume, all you have to do is use that one line instead of trying to find the current audioListener in the scene.

more ▼

answered May 10 '12 at 09:58 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

tl;dr

audioListener.volume = 0.0f;

becomes

AudioListener.volume = 0.0f;
May 10 '12 at 09:58 AM syclamoth

Converted way too much today. I see the issue, didn't realize that I was just being dumb. If I were not tired, this would have helped...

(from the API docs) - for anyone else who may find this useful.

This class implements a microphone-like device. It records the sounds around it and plays that through the player's speakers. You can only have one listener in a scene.

May 10 '12 at 10:03 AM JSorrentino

The docs are actually a bit misleading in this way- there is actually nothing stopping there from being more than one audioListener- all that happens is that you get a huge slew of error messages at runtime.

Basically, that's got nothing to do with the fact that it uses static variables for some of it's functionality- you still need an instance reference (which you are extracting properly, no problems there) in order to change 'velocityUpdateMode'.

May 10 '12 at 10:08 AM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4155
x1949
x4

asked: May 10 '12 at 09:55 AM

Seen: 592 times

Last Updated: May 10 '12 at 10:08 AM