I’m trying to “collect” an object, since I no longer need it in the world I delete it with “Delete(gameObject);” and it works perfect. Now I want to play a sound as well, so I have and it works but once the object is deleted the rest of the collectables can’t find the audioSource, even if I attach it to them… I’ve tried attaching the audio to an inanimate object like a Shovel that’s part of the scenery but it wont work. Can someone help me to properly attach audio and link it from another object?
I think my problem is that the script I am using is deleting the gameObject and anything it’s linked to/with.
var PageCount : GUIText;
var Count : int;
var Pickup : AudioSource;
function Touched(hit:RaycastHit)
{
Destroy(gameObject);
}
function Update () {
if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 0.8))
{
hit.transform.SendMessage("Touched", hit, SendMessageOptions.DontRequireReceiver);
Count ++;
PageCount.guiText.text = Count.ToString();
audio.Play();
audio.loop = false;
}
}
}