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 /
avatar image
0
Question by alleZSoyez · Dec 27, 2015 at 11:02 AM · scripting problemvariableaudiosourcepublicscope

Cannot access AudioSource from within function

Alrighty, I'm a newbie newb to Unity following Brackey's tutorial for making a Pong clone.

For the life of me I can't get the audio to play. The method used in the video caused the sound effects to play when the game started, even though "Play On Awake" is disabled. Well, that won't work. So I thought I'd manually call the audio myself via the existing functions.

I have a script "gamesetup" that handles the assets:

 #pragma strict
 var mainCamera : Camera;
 var topWall : BoxCollider2D;
 var bottomWall : BoxCollider2D;
 var leftWall : BoxCollider2D;
 var rightWall : BoxCollider2D;
 var player1 : Transform;
 var player2 : Transform;
 var scoreSound : AudioClip;
 var hitSound : AudioClip;
 ...

And the script "gameManager" that handles the scores:

 #pragma strict
 
 static var player1Score : int = -1;
 static var player2Score : int = -1;
 
 var textSkin : GUISkin;
 
 static function scoring (wallName : String) {
     if (wallName == "rightWall") {
         player1Score += 1;
         //Debug.Log("hit right");
     }
     else if (wallName == "leftWall") {
         player2Score += 1;
         //Debug.Log("hit left");
     }
     else {
         //Debug.Log("Je ne suis pas un mur");
     }
 
     //Debug.Log("P1: "+player1Score);
     //Debug.Log("P2: "+player2Score);
     gamesetup.scoreSound.Play();
 }

However, any approach I try to call the sound variable from within scoring() results in error:

Assets/gameManager.js(23,19): BCE0020: An instance of type 'gamesetup' is required to access non static member 'scoreSound'.

Explicitly declaring the AudioSource variables public did not work, and also prevented me from assigning the sound clip from the Inspector.

Declaring the variables static had a similar effect.

It does not appear to matter where I call the line gamesetup.scoreSound.Play();within that script; it just gives the same error.

Now, I've programmed before, mostly in PHP but nonetheless I'm not a complete noob. I'm pretty certain that if something is global/public/what have you by default, it means exactly that, and should not have this problem. Are individual scripts in Unity treated as their own scope? What am I missing?

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Access a variable from another script in update function 1 Answer

Can I declare a variable inside a function which has instance scope? 1 Answer

Scope issues when initializing variables 2 Answers

Script not responding to public variable change 1 Answer

Accessing variable from a method in another script and gameObject 2 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