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 Steven-1 · Jan 10, 2011 at 09:11 PM · audioloopplayoneshotendless

PlayOneShot keeps playing for ever (loops)

for easily(er) playing some soundeffects, I made a small script:

SoundController:

private static var Singleton : GameObject;

static function GetController() {
if (!Singleton) Create(); return Singleton; }

private static function Create() { Singleton = new GameObject("SoundObject"); Singleton.AddComponent(SoundScript); Singleton.AddComponent(AudioSource); }

static function Play(sound:AudioClip,volume: float)// = 1.0F) : void {
if (!Singleton) Create(); Singleton.audio.PlayOneShot(sound,volume);; }

So I can just do

SoundController.Play(sound,1);

Only problem is it keeps playing the sound over and over as soon as it first starts.

Why did I do wrong ?

EDIT:

answer: Nothing. I don't think I actually did anything wrong, cause it works now. Could it be a caching problem of some sort in Unity? (no idea how that would have this effect but I just don't know what else it could be)

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

1 Reply

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

Answer by Mike 3 · Jan 10, 2011 at 09:29 PM

Your whole script could be replaced with the much easier:

static function Play(sound:AudioClip, volume:float)
{
    AudioSource.PlayClipAtPoint(sound, Vector3.zero, volume); //could replace the position with Camera.main.transform.position or somesuch if you want it to play at a constant volume
}

You don't need a gameobject nor audiosource to use PlayClipAtPoint, since it's a static function, whch makes it easier for doing what you're trying to achieve

Either way, if that doesn't fix it, then the bug is likely in the class calling it

Comment
Add comment · Show 7 · 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 Statement · Jan 10, 2011 at 09:44 PM 0
Share

Incorrect. You need an instance to use PlayOneShot. However you probably refer to AudioSource.PlayClipAtPoint.

avatar image Statement · Jan 10, 2011 at 09:47 PM 0
Share

But I agree that the script probably can be replaced all together.

avatar image Mike 3 · Jan 11, 2011 at 01:38 AM 0
Share

Oh true, this is why you shouldn't answer on Answers after 40 hours working without sleep! Fixed it :D

avatar image Steven-1 · Jan 11, 2011 at 06:47 PM 0
Share

yup, that works. I didn't know PlayClipAtPoint existed, that makes my entire script useless. thanks.

avatar image Steven-1 · Jan 11, 2011 at 06:55 PM 0
Share

I don't really get the position parameter though, it doesn't seem to have any effect.

Show more comments

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

No one has followed this question yet.

Related Questions

Audio after Audio Loop 1 Answer

my audio is not looping even when it is set to loop 5 Answers

Can I play multiple AudioSources from one gameobject? 8 Answers

My AudioClip wont loop even though loop is true 1 Answer

My soundclip is noisy? 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