Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PeCcHy · Nov 02, 2016 at 02:43 AM · javascriptaudiosourceaudio.play

"BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member" while trying to play an AudioClip

Here is my script:

   #pragma strict

 var maxFallDistance = -10;
 var a = false;
 var audio : AudioSource = gameObject.GetComponent(AudioSource);
 
 function Update () {
     if (transform.position.y <=maxFallDistance && a == false){
         
         RestartLevel();
         
 
     };
 };
 
 function RestartLevel ()  {
    
     UnityEngine.Component.audio.Play();
     yield WaitForSeconds (4);
     Application.LoadLevel("Level01");
     GameMaster.currentScore = 0;
     a = true;
 
 };

Whenever I try to start the game I get the error from the title telling my that an instance of type 'UnityEngine.Component' is required to access non static member 'audio' and redirecting me to UnityEngine.Component.audio.Play();. Please help I've been stuck on this the whole day I'm about to cry. bump i guess

Comment
Add comment · Show 1
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 PeCcHy · Nov 02, 2016 at 05:36 PM 0
Share

Anyone that can help me? Still haven't solved it

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Nov 03, 2016 at 03:03 PM

You "SHOULD" set the variable in the Awake or Start function. Couple other comments about semi-colon use below and calls to the audio source reference.

  #pragma strict
  var maxFallDistance = -10;
  var a = false;
  var audio : AudioSource;
 
 function Start () {
     audio = gameObject.GetComponent.< AudioSource >(); // Get the actual type, if you do it as you did in the example in your post, it will return a Component type which is not a AudioSouce.
 }
 
  function Update () {
      if (transform.position.y <=maxFallDistance && a == false){
          RestartLevel();
      } // don't put a semi-colon here
  } // don't put a semi-colon here
  
  function RestartLevel ()  {
      audio.Play();  // Just use the AudioSource reference you set in Start
      yield WaitForSeconds (4);
      Application.LoadLevel("Level01");
      GameMaster.currentScore = 0;
      a = true;
  
  } // don't put a semi-colon here

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 PeCcHy · Nov 04, 2016 at 12:03 AM 0
Share

What do I need to get the actual type like you say in line 7? If i just copy and paste your code I still get the BCE0004: Ambiguous reference 'audio': BallHealthGameOver.audio, UnityEngine.Component.audio. error. And also, how do I use the AudioSource reference I set in start ins$$anonymous$$d of audio.Play()

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

106 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound not Playing on Left Mouse Button being pressed,Sound Not Playing when I click the left mouse button 0 Answers

Why won't my second Audio Source play? 0 Answers

Audio doesn't sound 0 Answers

audio is not playing during IEnumerator 1 Answer

Audio Fade In / Fade Out with Trigger 0 Answers


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