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 phatpixels · Jul 15, 2018 at 12:19 AM · audiosourcebooleansetactiveplayoneshotaudio.play

play sound only once on SetActive of GameObject

hello, i have looked up and down and tried various scripts, but i cannot seem to get audio to play only once on each time the object is SetActive. i have tried variations of playOneShot etc, but cannot get it to play audio 'once' each time the object is setactive. The particular gameobject randomly switches SetActive true/false. i would like the audio to play once each time the gameobject is setactive.(true). Hope this makes sense, any leads would be greatly appreciated :)

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 Stratosome · Jul 15, 2018 at 07:45 AM

Hrmmm, well, assuming that it isn't the audio itself you are having trouble with, and PlayOneShot is making a sound, you could put that audio function inside of the OnEnable() function. Anything inside of that function will happen once every time the object is enabled. There is also a OnDisable() function in case it is useful.

Comment
Add comment · Show 1 · 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 phatpixels · Jul 15, 2018 at 10:48 AM 0
Share

boom!...it worked like a charm. thankyou so much.

avatar image
0

Answer by Kawalyn · Jul 15, 2018 at 10:31 AM

I put together a simple script that might help. Hope it makes sense, feel free to ask if youve got another question :)


EDIT: Just to clarify, this script can be attached to an empty gameobject, you then tell the script which gameobject(s) you want to activate/deactivate upon a certain condition.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SetActiveScript : MonoBehaviour {
 
     public GameObject objToActivate;
     AudioSource attachedAudioSource;
     
     void Start () {
         attachedAudioSource = objToActivate.GetComponent<AudioSource>();
     }
 
     void Update () {
         // on left mouse button (by default)
         if (Input.GetButtonDown("Fire1"))
         {
             // check if object is active and if audio is playing
             if (objToActivate.activeSelf && !attachedAudioSource.isPlaying)
             {
                 // deactivate object
                 objToActivate.SetActive(false);
             }
             // check if object is deactivated
             else if (!objToActivate.activeSelf)
             {
                 // activate object if it was deactivated
                 objToActivate.SetActive(true);
                 // make sure audiosource isnt playing already for some reason
                 if (!attachedAudioSource.isPlaying)
                 {
                     // make sure audio is not looping
                     attachedAudioSource.loop = false;
                     // play audiosource
                     attachedAudioSource.Play();
                 }                
             }
         }        
     }
 }
 

Comment
Add comment · Show 2 · 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 phatpixels · Jul 15, 2018 at 10:47 AM 0
Share

@$$anonymous$$awalyn thanks so much for the script, i'll be sure to implement it for my little sound game i am making. Being a creative i don't know too much about scripting, but i was able to use the OnEnable function for this occasion. $$anonymous$$uch appreciated. Thankyou.

avatar image Kawalyn phatpixels · Jul 15, 2018 at 10:52 AM 0
Share

Youre welcome^^ Totally forgot about the OnEnable function, way easier i guess. Good luck with your sound game!

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

91 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 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

master background music toggle not working after changing scenes 1 Answer

Making a sound play only once with a Boolean variable in javascript 1 Answer

audio.Play(); not working while audio.Stop(); works fine when the audio is played on awake 1 Answer

How to determine which audiosource to play from when I'm using two at once. 1 Answer

Why some audio files are not working in Unity? 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