Hey guys, before I start, I gotta warn you, I’m no scripting pro, so I hope this doesn’t seem to basic or anything.
After some help froma friend and tinkering around in the scripting referance, I ALMOST got audio to play when I pick up an item.
This script is located in the item that I’m going to pick up.
var points : int;
var aclip : AudioClip;
var colcount : boolean;
function Update () {
}
function OnTriggerEnter (col : Collider) {
var playerStatus : PlayerStatus = col.GetComponent(PlayerStatus);
//audio.PlayOneShot(aclip);
playerStatus.AddPoints(points);
if(colcount)
ObjectCollect.collect++;
Destroy(gameObject);
audio.Play();
}
The only problem is that it won’t play the audio. There’s a place to drop an audio clip in the inspector, but it just doesn’t play. I’ve tried moving the audio.Play line to different points, but the project either breaks or the audio doesn’t play… and sometimes the object doesn’t even get picked up/
Try cranking up the volume of the OneShot. I had a similar problem in my game, where the distance from the sound to the camera was too large, so it was actually working fine but the sound was too quiet to be heard. Took me a while to figure it out.
Just tack a large number onto the end of the function’s input… e.g.
The problem is that you destroy the gameobject which is actually playing the sound. First of all: place your audio code above your destruction code. I’m not 100% sure it will actually play to the end when the gameobject is destroyed. If it does not, create a prefab which contains a gameobject with an audiosource and the following script attatched to it:
function Start() {
audio.loop = false;
audio.Play();
}
function Update() {
if(!audio.isPlaying)
Destroy(gameObject);
}
So in your previous script instead of the current audio.Play() you place this: