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 Dqimass · May 25, 2016 at 06:09 AM · 2d gameaudiosourceaudioclipclickaudiolistener

Audio won't play sound

Hey sorry guys, this may sound simple but I just don't understand why the audio clip not playing. I did...

source.PlayOneShot (sound);

source.clip = sound;

source.play();

but none ever worked. The message is saying "Object reference not set to an instance of an object" but what does it mean, I did throw in the audio sound into the public audiosource slot.

 public AudioClip sound;
 private AudioSource source;

 void awake()
 {
     AudioSource source = GetComponent<AudioSource>();

 void Update()
     target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     if ((grounded || !doubleJump) && Input.GetMouseButtonDown (0)) {
         if (target.x <= transform.position.x) {
              source.PlayOneShot (sound, 1f);

where did i go wrong? is it cause of the audiosource file. the sound I think it's only 0.30 sec long. it's sounded like bloop.

`

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

2 Replies

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

Answer by Alec-Slayden · May 25, 2016 at 06:36 AM

"Object reference not set to an instance of an object" means you are trying to use something that is null (i.e. null reference exception).

First, make sure you capitalize "Awake()" or it will not be called, and then you will not be retrieving your audio source, so it will remain null.

Then, make sure you close the bracket for the Awake method so it compiles properly, and add brackets to the update method as well... I'm assuming this was done in the original code or it wouldn't have been able to run at all.

Next, make sure you actually have an AudioSource component on the object, otherwise when Awake() gets called it won't find anything.

If you still get the null reference exception after doing these things, Post the line it refers to in the error and the surrounding code, as it may not necessarily be related, or there may be something else wrong.

Comment
Add comment · Show 3 · 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 Dqimass · May 25, 2016 at 06:41 AM 0
Share

thank you! i'll try

avatar image Dqimass · May 25, 2016 at 07:43 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object Diving.Update () (at Assets/Script/Diving.cs:67)

it also says that source is assigned but its value is never used but i thought i am using it with

source.PlayOneShot (sound, 1F);

also Assets/Script/Diving.cs(34,29): warning CS0649: Field Diving.source' is never assigned to, and will always have its default value null'

avatar image Dqimass · May 25, 2016 at 08:00 AM 0
Share

Ahh thanks a lot, I fixed it. It was the awake that causing the problem, i had to go back and retry everything and the source.PlayOneShot works now. thanks so much

avatar image
0

Answer by akisrn · May 25, 2016 at 03:13 PM

Try this:

  1. Create a GameObject on your Hierarchy.

  2. Click 'Add Component' then add "Audio Source"

  3. Drag the sound you want to the 'Audio Clip' part. (Also, dont forget to tick off the "Play on Wake" box)

  4. Create another GameObject where you will attach your script. From there, you will be asked to put the source of your 'source'. Drag the first GameObject you created to the slot for 'source'

Comment
Add comment · 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

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

48 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

Related Questions

Why do AudioClips still play with no Audiolistener? 0 Answers

How to load audio from server url 1 Answer

AudioSource volume inconsistency 1 Answer

How to play multiple sounds simultaneously 2 Answers

Audio: -3db automatic attenuation on any audio playing? 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