- Home /
How to access audio of FindChild Object & play it? Error
Hi Guys,
I'm trying to let the trigger box collider's script play audio from its own child, door's audio but it's not working.
But trigger box collider's script (TriggerZone_js) is not recognizing var, lockedSound from the child door's audio
I have dragged the audio for the lockdedSound to my script's lockedSound
I believe because I didn't declare it correctly (comments // Error Msg),
1) What is the correct way of accessing child object's audioClip & play it (`PlayOneShot`). I also tried to follow from this, http://answers.unity3d.com/questions/332262/how-to-play-audio-nested-in-an-object.html , it's returning the same error message, `Unknown identifier: 'door_locked'.`
Below is my trigger Box Collider's script, TriggerZone_js.js:
#pragma strict
var trilockedSound : AudioClip;
function Update () {
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag == "Player")
{
if(Inventory_js.charge == 4)
{
//add another if else statement if you want it with OnTriggerStay
//& get door's script if Door is Open or not
transform.FindChild("door").SendMessage("DoorCheck");
Debug.Log("Ontriggerstay is working");
}
else
{
transform.FindChild("door").audio.PlayOneShot(lockedSound); // Error Msg
// audio.PlayOneShot(trilockedSound); // can only play it within the trigger obj
col.gameObject.SendMessage("HUDon");
}
}
}
Below is my child object, door's script DoorManager_js.js:
#pragma strict
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
var lockedSound : AudioClip;
var aniPlayed: boolean = false;
I also noticed that if I change my script (TriggerZone_js.js):
// from this
transform.FindChild("door").audio.PlayOneShot(lockedSound);
// to this
transform.FindChild("door").audio.PlayOneShot(trilockedSound);
It can play it's OWN audio trilockedSound, I thought if I write Findchild("child").audio.PlayOneShot(AudioVar).
It would play from the child's audio instead it's own audio. 2) What is going on here?
The Error Message is:
Unknown identifier: 'lockedSound'.
1) What is the correct way of accessing child object's audioClip & play it (PlayOneShot)
2) Why FindChild Play audio is not playing child's audio, yet play current object's audio instead?
3) Is there anything else I need to know to understand or to make it better?
Please let me know if I need to provide any more information, thanks!
Others information: Unity ver 3.5.6f4
I tried to use `getComponent` it didn't work very well. It still requires a variable to contain the `"lockedSound"` then it will works. if not it's still cannot recognize "`lockedSound`".
Any advice?
Below is my attempted solution:
var testlockedSound : AudioClip;
var doorchild : Transform;
var scriptName : Door$$anonymous$$anager_js;
function Start () {
doorchild = transform.Find("door");
scriptName = doorchild.GetComponent(Door$$anonymous$$anager_js);
testlockedSound = scriptName.lockedSound;
Debug.Log(testlockedSound);
}
function Update () {
// audio.PlayOneShot(testlockedSound); //it works with a var to contain it.
scriptName.audio.PlayOneShot(lockedSound); // it still cannot recognize lockedSound
}