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 /
  • Help Room /
avatar image
0
Question by Pedro Garcia-Huidobro · Mar 18, 2016 at 09:03 PM · triggersoundssource

Playing Audio Source on trigger not working

Im trying to make a song play when the player character passes a collider, but i can't manage to make it posible :c I have been using the ActivatreTrigger Standar Asset and ....

 using System;
 using UnityEngine;
 using Object = UnityEngine.Object;
 
 namespace UnityStandardAssets.Utility
 {
     public class ActivateTrigger : MonoBehaviour
     {
         // A multi-purpose script which causes an action to occur when
         // a trigger collider is entered.
         public enum Mode
         {
             Trigger = 0,    // Just broadcast the action on to the target
             Replace = 1,    // replace target with source
             Activate = 2,   // Activate the target GameObject
             Enable = 3,     // Enable a component
             Animate = 4,    // Start animation on target
             Deactivate = 5  // Decativate target GameObject
         }
 
         public Mode action = Mode.Activate;         // The action to accomplish
         public Object target;                       // The game object to affect. If none, the trigger work on this game object
         public GameObject source;
         public int triggerCount = 1;
         public bool repeatTrigger = false;
 
 
         private void DoActivateTrigger()
         {
             triggerCount--;
 
             if (triggerCount == 0 || repeatTrigger)
             {
                 Object currentTarget = target ?? gameObject;
                 Behaviour targetBehaviour = currentTarget as Behaviour;
                 GameObject targetGameObject = currentTarget as GameObject;
                 if (targetBehaviour != null)
                 {
                     targetGameObject = targetBehaviour.gameObject;
                 }
 
                 switch (action)
                 {
                     case Mode.Trigger:
                         if (targetGameObject != null)
                         {
                             targetGameObject.BroadcastMessage("DoActivateTrigger");
                         }
                         break;
                     case Mode.Replace:
                         if (source != null)
                         {
                             if (targetGameObject != null)
                             {
                                 Instantiate(source, targetGameObject.transform.position,
                                             targetGameObject.transform.rotation);
                                 DestroyObject(targetGameObject);
                             }
                         }
                         break;
                     case Mode.Activate:
                         if (targetGameObject != null)
                         {
                             targetGameObject.SetActive(true);
                         }
                         break;
                     case Mode.Enable:
                         if (targetBehaviour != null)
                         {
                             targetBehaviour.enabled = true;
                         }
                         break;
                     case Mode.Animate:
                         if (targetGameObject != null)
                         {
                             targetGameObject.GetComponent<Animation>().Play();
                         }
                         break;
                     case Mode.Deactivate:
                         if (targetGameObject != null)
                         {
                             targetGameObject.SetActive(false);
                         }
                         break;
                 }
             }
         }
 
 
         private void OnTriggerEnter(Collider other)
         {
             DoActivateTrigger();
 
         }
     }
 }

a script I found online

 using UnityEngine;
 using System.Collections;
 
 public class SoundTrigger : MonoBehaviour {
 
     public AudioSource source;
     public AudioClip clip;
     private bool isPlayed;
 
     void Start(){
         source = GetComponent<AudioSource> ();
         isPlayed = false;
     }
 
     void OntriggerEnter(Collider other){
         if (!isPlayed) {
             source.Play ();
             isPlayed = true;
         }
     }
 
     void OntriggerExit(Collider other){
         if (source.isPlaying) {
             source.Stop ();
         }
     }
 }



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

Answer by MacroNerd · Mar 18, 2016 at 09:31 PM

  1. Create a new plane (if it's 3D) and set the mesh collider to Convex and Is Trigger.

2.)Set the plane's tag to Trigger or whatever you can remember.

3.)Now this code may work, but you should test it and if it doesn't, then sorry. Made it off the top of my head..

 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Trigger")) 
         {
             Component.audio.enabled = true;
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag ("Trigger")) 
         {
             Component.audio.enabled = true;
         }
     }
 
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 Pedro Garcia-Huidobro · Mar 20, 2016 at 01:50 PM 0
Share

it didn't work :c thanks anyway : )

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

58 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

Related Questions

Can't figure out how to play a random audio clip when player collides with enemy 1 Answer

Collider trigger two times, even if it's not active. 1 Answer

OnTriggerEnter2D being called 25 times. 0 Answers

Gameobject doesnt move after build 0 Answers

how to load panels in a platformer game? 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