- Home /
Script unattached to Object will not compile
I feel like I've hit a catch twenty two:
I am creating a sound script that will instance a whirring noise only when a turret is moving to line up with it's target. I have two scripts that accomplish this. The idea revolves around a Vector3.Angle() function that determines the difference between where the turret is pointed and where it thinks it needs to go. If that value has a nonzero value than it is assumed the turret is in movement. This value is assigned to a var called float_angle.
Soundcounter is a modulus 10 integer that is constantly moving. On the value of soundcounter == 9 the float_angle function updates. On soundcounter == 10 the following script is set to function:
function soundtrigger () {
if(soundcounter == 10 && float_angle == 0)targetObject.SendMessage(stop);
else(soundcounter == 10 && float_angle > 0)targetObject.SendMessage(movement);
}
This informs the following function:
SentrySoundManager.js
var start_rotate : AudioClip;
var end_rotate : AudioClip;
private var looping : boolean = false;
function loopState(looping: boolean){
audio.loop = looping;
}
function movement(){
if (!AudioSource.isPlaying){
looping = true;
audio.PlayOneShot(start_rotate);
}
AudioSource.loop = looping;
yield WaitForSeconds(start_rotate.length);
audio.Play();
}
function stop(){
if (AudioSource.isPlaying){
looping = false;
audio.PlayOneShot(end_rotate);
}
}
My issue is that the SentrySoundManager.js will not compile, and further complicates the unity scene by making the rest of the scripts unavailable until the compilation error is resolved. I get the following message in the Console of Unity:
"Assets/WeaponScripts/SentrySoundManager.js(9,26): BCE0020: An instance of type 'UnityEngine.AudioSource' is required to access non static member 'isPlaying'.
Assets/WeaponScripts/SentrySoundManager.js(13,21): BCE0020: An instance of type 'UnityEngine.AudioSource' is required to access non static member 'loop'.
Assets/WeaponScripts/SentrySoundManager.js(19,25): BCE0020: An instance of type 'UnityEngine.AudioSource' is required to access non static member 'isPlaying'."
I don't understand how to instance an AudioSource for a script that is not attached to an Object in the scene. Am I interpreting this incorrectly? Thanks, and if I can clarify anything lemme know.
Answer by Seth-Bergman · Mar 13, 2013 at 06:55 AM
you need to tell the script which AudioSource in the scene you are talking about.. if (as I assume) the audio source is on the same object as the script, change "AudioSource" to simply "audio":
var start_rotate : AudioClip;
var end_rotate : AudioClip;
private var looping : boolean = false;
function loopState(looping: boolean){
audio.loop = looping;
}
function movement(){
if (!audio.isPlaying){
looping = true;
audio.PlayOneShot(start_rotate);
}
audio.loop = looping;
yield WaitForSeconds(start_rotate.length);
audio.Play();
}
function stop(){
if (audio.isPlaying){
looping = false;
audio.PlayOneShot(end_rotate);
}
}
if,alternately, the audio source you were after were on another object, like say the turret itself:
var otherAudio = GameObject.Find("turret").GetComponent(AudioSource);
if(otherAudio.isPlaying)...etc
Your assumption is correct about the scripts' placement.
I cleared the error I was encountering; thank you for the pointer. Additionally the otherAudio paradigm is helpful too.