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 MobinS · Aug 03, 2011 at 11:54 PM · triggersound

On trigger , sound play

hi I Made a cube underwater as triger... then I wrote this script :

 using UnityEngine;

using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

 MotionBlur MB;
 void Start () 
 {
     MB = (MotionBlur) gameObject.GetComponentInChildren<MotionBlur>();
     
     }
 
 void OnTriggerEnter()
 {
         
     MB.enabled = true;
     
     
 }
 
 void OnTriggerExit ()
 {
     MB.enabled = false;
     

} }

so when I go under water (trigger cube) motion blur will enabled... but I also wanted to Play a sound when I get to that triger... like under water effect... I know I should use play.sound , but how can I direct it to my specific sound?

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 crazyKnight · Aug 04, 2011 at 10:24 AM

 using UnityEngine;
 using System.Collections;
 
 
 public class Test : MonoBehaviour {
 
     AudioClip MyClip;
     
     void Start () {
     
     }
     
     void Update () {
     
     }
     
     void OnTriggerEnter(Collider info)
     {
         if(info.gameObject.tag == "Tag Name of our object")
         {
             audio.Play();
         }
     }
     
     void OnTriggerExit(Collider info)
     {
         if(info.gameObject.tag == "Tag Name of our object")
         {
             audio.Stop();
         }
     }
 }
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
avatar image
0

Answer by roamcel · Aug 04, 2011 at 11:07 AM

First of all, you must add an audio source to your cube-collider object.

Secondly, I'd rather suggest you create a simple script called "SoundEffect" that you attach to the water (pay attention to the capital letters when defining variables and class names. with this single instruction inside

var audioClip : AudioClip;

Then you go and drop the "splash.wav" file you've chosen for the audio splash effect into the inspector.

In your code, OnTriggerEnter MUST be redeclared with the collider info like crazyknight suggests, and then you simply add there

var soundeffect: SoundEffect = info.GetComponent(SoundEffect); //SoundEffect is the name of the class you created and that you attached to the water
audio.clip = soundeffect.audioClip;
audio.Play();

Assigning the "SoundEffect" component to other objects will let you play audio when the cube-collider object interacts with them, too.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Sound Trigger 2 Answers

How to have audio only be heard in a certain radius? 4 Answers

How to get sound to play on barrel Explosion 1 Answer

Trigger sound when walking through cube trigger 3 Answers

Object Sound Trigger 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