Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moonLite · Nov 24, 2012 at 04:07 PM · javascriptaudiofindfindchild

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Nov 24, 2012 at 06:54 PM 0
Share

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
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Removing specific child objects 1 Answer

Get a list of children (in order) 1 Answer

How can I make a playlist last the whole gameplay? 1 Answer

Why does the audio sound different than the original ? 0 Answers

Destroying Collected Objects / Playing Audio 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges