- Home /
Playing sound through pickup trigger
Hey guys, I'm a bit confused to why my sound is not playing when I go through an object
using UnityEngine;
using System.Collections;
public class Jewels : MonoBehaviour
{
public GUIText jewelsText;
public GUIText winText;
public AudioClip Pickup;
private int jewels;
// Use this for initialization
void Start ()
{
jewels = 0;
SetJewelsText();
winText.text = "";
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Pickups")
{
other.gameObject.SetActive(false);
jewels = jewels + 1;
SetJewelsText();
}
if (Pickup)
{
audio.clip = Pickup;
audio.Play();
}
}
void SetJewelsText()
{
jewelsText.text = "jewels:" + jewels.ToString();
if(jewels >=20)
{
winText.text = "You Collected all the jewels!";
}
}
}
I just did the Roll a Ball Tutorial and am trying to figure out how to do the same thing. Call a sound when I pick up each cube. Does this code need to be placed in the "Player" script I have currently or can I make a new Pick Up's script ?
Thanks, $$anonymous$$risti
Answer by Mint92 · Nov 14, 2013 at 11:17 PM
I get an error "There is no AudioSource attached to the "Player" game object, but a script is trying to access it.
post comments using the "add new comment" button. Delete your accidental answer when you get a chance.
Answer by Psycho8Vegemite · Mar 29, 2016 at 06:42 PM
Make sure that both gameObjects have a collider, and that the gameObject that is tagged "Pickups", has 'Is Trigger' set to true.
Also on either gameObject, one of them should have a rigidbody to actually register collisions. Due to one of the colliders being set to 'Is Trigger'. The rigidbody won't react to it, just register it and therefore have 'OnTriggerEnter()' be called.
Along with this I changed the audio to use an AudioSource:
GetComponent<AudioSource>().whatever
so add that on as well unless your not using Unity5; if so 2D/3D sound can be achieved with the 'Spatial Blend' within the component.