• 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
Question by Konkor4 · Oct 14, 2012 at 08:10 PM · audiosoundplaydelete

gameObject to play sound on deletion, won't work...

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;
       }
    }
     
 }
Comment

People who like this

0 Show 3
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 dannyskim · Oct 14, 2012 at 10:42 PM 0
Share

You're going to have to specify what code is actually part of this script. Bunching everything into one code block without describing stuff isn't going to give us any insight on how to help you solve your problem.

avatar image Konkor4 · Oct 14, 2012 at 10:44 PM 0
Share

I'm not entirely sure what you mean? I always keep my scripts small so that I don't mix bits up. This is the script for collecting the pages, it's pretty self explanatory since it's a basic raycast/delete script.

avatar image dannyskim · Oct 14, 2012 at 10:51 PM 0
Share

So, from my perspective trying to look at your code, the RayCast is being tracked by the same script that it's SendMessaging to Destroy. So, take for example if you had this script on a cube in an empty scene, you click on it and the SendMessage is sent to execute Touched, you're going to immediately Destroy() the object.

That in essence means any code that comes after the SendMessage is probably not going to execute since the GameObject is being destroyed.

That's where my confusion is coming from.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by T27M · Oct 14, 2012 at 11:25 PM

Change the type of Pickup to AudioClip assign the clip your trying to play in the inspector and use the audio.PlayOneShot().

 var Pickup : AudioClip;
 
 if (Physics.Raycast (ray, hit, 0.8))
       {
         hit.transform.SendMessage("Touched", hit, SendMessageOptions.DontRequireReceiver);
          Count ++;
          PageCount.guiText.text = Count.ToString();
          audio.PlayOneShot(Pickup);
         
       }

You need to make sure the object that is playing the sound also has a AudioSource component attached to it.

Comment

People who like this

0 Show 0 · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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 avatar image avatar image

Related Questions

Audio Playing upon death 1 Answer

Audio sound on gameObject delete? 1 Answer

Audio wont play when conditions are met 1 Answer

Background Music 2 Answers

Make door play sound when you open and close it ? 1 Answer


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