Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnTrigger Audio Effect-Need Help

Discussion in 'Audio & Video' started by Nikola310, Oct 28, 2015.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hello everyone.I need help about triggering a sound when my Player object picks up an item.(I am making my more advanced version of Roll A Ball)
    I have been searching for four days and I can't find the answer.I have put an Audio Source component in my Enemy Prefab(when you pick up an enemy the "no" sound should play"),and put my sound in there.
    Thank you in advance
     

    Attached Files:

  2. michaelhartung

    michaelhartung

    Joined:
    Dec 19, 2013
    Posts:
    72
    The "audio" variable in your script is a reference to the Audio Source of the object you have this script attached to. Since the script is called "PlayerController" I assume it's attached to your player object and not the enemy object. So you need sth. like:

    Code (csharp):
    1. audio = other.gameObject.GetComponent<AudioSource>();
    2. audio.Play();
    Hope that helps!
     
  3. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    I have two objects:One gives you a point,One gives you minus one point when collecting it.Since there isn't an audio tutorial in the Roll A Ball Tutorial series,I don't really know where to attach the AudioSource component.I've attached it both to the Player & the point-objects.I have two audios,one "no" that plays when the Player collects a NegativePoint-Collectible,and a "Nom nom nom" audio that plays when the Player collects a +1Point Collectible.Should I create a seperate script for audio?And if so,how can I "define"(call) multiple AudioSources from other Game Objects in one script?Thanks in advance,and sorry for the confusion.
     
  4. michaelhartung

    michaelhartung

    Joined:
    Dec 19, 2013
    Posts:
    72
    You attach the AudioSource component to the respective object prefabs. Then in your PlayerController script you detect when you collide with one of these objects (OnTriggerEnter). The code I posted before shows you how to access the AudioSource Component of the objects you collide with within that function and play them.