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 greatUnityGamer · Aug 19, 2013 at 04:38 PM · audio

How do you Play sound without having to load it In Inspector?

So i'm gonna have many characters of different types, and they may share sounds they emit.

So dragging sounds to the inspector 1 by 1, for every character type in my scene is tedious.

Can you just access any sound in your Assets and play it from code?

So far what i got is giving me errors saying: Cannot implicitly convert type UnityEngine.Object to UnityEngine.AudioCLip

 using UnityEngine;
 using System.Collections;
 
 public class PlaySouuunn : MonoBehaviour  {
     AudioClip vv = Resources.Load("Blow");
 
 void Start(){
         audio.PlayOneShot(vv);
 }
 void Update(){
     //do an if or whatever statement
     
 }
     
 }

This one also doesn't work AudioSource.PlayClipAtPoint(clip, transform.position);

So is this the line that has the problem? ---> AudioClip vv = Resources.Load("Blow"); I got this code from a example online, it was an answer here in UnityAnswers i think. but they had it in javascript as:

 function PlayFile(file: String){ // file name without extension
     var clip: AudioClip = Resources.Load(file);
     // example 1: play with PlayOneShot
     audio.PlayOneShot(clip);
     // example 2: play without an AudioSource component
     AudioSource.PlayClipAtPoint(clip, transform.position);
     // example 3: play with Play
     audio.clip = clip;
     audio.Play();
 }

So you see i'm basically doing the same

 Auddioclip vv = Resources.Load

and then the play.

And yes i have a Resources folder in my assets folder

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
2
Best Answer

Answer by Sajidfarooq · Aug 19, 2013 at 04:41 PM

You need to cast the return value from Resource.Load into an AudioClip.

 AudioClip vv = Resources.Load("Blow") as AudioClip;

Also remember to put the audio clip in a folder called "Resources", since that is where the Resources.Load expects it to be.

Comment
Add comment · Show 4 · 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 greatUnityGamer · Aug 19, 2013 at 08:38 PM 0
Share

Still doesn't work.

It says There is no AudioSource attached to the BBB game object, but a script is trying to access it.

avatar image scipiothegreat · Aug 20, 2013 at 01:28 AM 0
Share

AudioSource is required for any sound and wont be affected by however you set up the sound clips. Just add it from Component > Audio > Audio Source

avatar image Sajidfarooq · Aug 20, 2013 at 01:34 AM 0
Share

You need to have an "AudioSource" component attached to the gamebject you want to play the audio file from.

Read this and read this to understand how to use an Audio Source and the difference between an AudioSource and an AudioListener.

avatar image Sajidfarooq · Sep 06, 2013 at 12:08 PM 0
Share

Remember to mark this question as "answered" if it has indeed answered your question.

avatar image
0

Answer by kwokman · May 29, 2015 at 09:18 AM

You can try the simple:

AudioSource audio = gameObject.AddComponent < AudioSource > ();

audio.PlayOneShot ((AudioClip)Resources.Load ("blow"));

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to emit a sound on specific collision. 1 Answer

Audio Play Once 2 Answers

Can anyone help me invert this simple script? 2 Answers

Fade In/Out Audio After Enter Trigger 3 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