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
1
Question by Kyle 6 · Dec 08, 2010 at 04:14 PM · audiorandomaudioclip

Assign clip to audio source

Hello,

I have a prefab with an audiosource attached. The AudioClip variable box is empty, I want to assign it using a script.

Here's what I'm using to do that:

var clips : AudioClip[]; var rand = false;

function Start() { audio.clip = clips[Random.Range(0,clips.Length)]; RandSong(); } function Update() { if(rand) { RandSong(); rand = false; } } function RandSong() { audio.Play(); yield WaitForSeconds (audio.clip.length); audio.clip = clips[Random.Range(0,clips.Length)]; rand = true; }

Then, on the same prefab, I fill in a few slots of the clips[] array with music. However, nothing plays when I run my game.

Can anyone help me out?

Comment
Add comment · Show 2
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 Jesse Anders · Dec 08, 2010 at 10:19 PM 1
Share

It's not immediately clear to me based on what you posted why you're not hearing anything. $$anonymous$$aybe someone else will spot the problem, but in the meantime, I'd recommend doing some debugging. Do the tracks play correctly if you just attach them to an audio source component with 'play on awake' checked? Were the clips imported with '3D Sound' set to 'on'? Are they playing, but are just too far away from the listener to be audible? And so on.

avatar image Argenex · Dec 14, 2010 at 05:49 AM 0
Share

Like Jesse asked; Did you attach the clip to the same part of the prefab where the script that calls to it is attached? Sounds like you did assign them in the Inspector window (need to do that as well). Also, if your camera isn't near the actual location of the clip (inherited transform of the objects within the parent obj), you won't hear it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TiagoSerra · Oct 13, 2017 at 02:41 PM

Here Mate

Instructions: 1 - attach this to any gameobject 2 - place the gameobject with the AudioSource component to mySource 3 - Lets say you have 3 audio clips you want to use, set both Range Scan and My Audio Elements to 3 4 - add your 3 clips as elements 5 - Enable Debug to get a list of the clips on the console but they also show on the editor as you play the game

Tips: this is on enable, so everytime this object is enable its gonna run a new range and assign the clip, Hope it helps took me about 5 minutes, if this helps you please consider following my facebook dev group HERE

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class randomSound : MonoBehaviour {

 public AudioSource mySource;
 public int rangeScan;
 public AudioClip[] myAudio;
 public int toPlay;
 public bool debugging;

 void OnEnable () {
     toPlay = Random.Range(0,rangeScan);
     if (debugging) {
         foreach (AudioClip value in myAudio) {
             print (value);
         }
     }
     mySource.PlayOneShot(myAudio[toPlay], 0.9F);
     mySource.Play ();
 }

}

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

1 Person is following this question.

avatar image

Related Questions

Need help to play a random explosion sound from an array. 1 Answer

Stop audio from looping and play at lower volume 1 Answer

Playing audio from a GUI only app 0 Answers

Receiving float[][] instead of float[] for AudioClip.PCMReaderCallback 1 Answer

Loading MP3's into an Audio Array and assigning them to members of a GameObject Array 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