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!";
		}
	}
	
}

Are you sure your Collider is set to isTrigger?

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, Kristi

2 Answers

2

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.

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.