|
Sorry for the newbie question. I have noticed that this gives me an error ('enabled' is not a member of 'UnityEngine.Component': mainCamera.GetComponent(AudioListener).enabled = false; But this seems to be work fine: var myListener:AudioListener = mainCamera.GetComponent(AudioListener); myListener.enabled = false; Is there a way to access the component without using a variable?
(comments are locked)
|
|
The way to do it without creating a variable is to cast the Component to an AudioListener:
(comments are locked)
|
|
The compiler is right, "enabled" is not a member of Component. AudioListener inherits from Component, so it's got some extra things. But if you look at the docs for GetComponent, you'll notice that it returns a Component. What you're doing in your second example is implicitly casting the component that is returned as an AudioListener. The explicit way to do it would be
Ad any rate, you can safely expect that when you call GetComponent with the type AudioListener, it can be cast as an AudioListener. If you don't want to explicitly declare a temporary variable, the following should work:
My personal preference is to leave the temporary variable, as it's more human-readable, and a little bit clearer what you're doing without all those parentheses, but that's just me (and probably a number of other people, as well). That's C# syntax, which won't work in Javascript.
May 05 '10 at 11:24 PM
Eric5h5
Whoops, you're correct. C# on the brain.
May 06 '10 at 12:43 PM
burnumd
(comments are locked)
|
