Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cogswellSteve · Mar 13, 2013 at 06:16 AM · javascriptaudioaudiosource

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cogswellSteve · Mar 13, 2013 at 08:40 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JavaScript Play Audiosource Problem 1 Answer

Why does the audio sound different than the original ? 0 Answers

How do I prevent audio from restarting if it's being asked to play again? 2 Answers

background Audio plays in Unity Editor but does not play in web player 1 Answer

Sound wont play when triggered (javascript) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges