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 Saiura · Jun 13, 2014 at 08:16 AM · gameobjecttriggerdestroycoroutinesound

Problem with timed code-execution (audioplay and object destruction)

I am trying to create a game-object that increases a counter and plays a sound on being triggered and then being destroyed. Think of it like the Coins in Mario and alike.

I do have my Script for the object which at the current state, increases the counter (Clartiy) just once, plays the sound, then turns the object invisible. Planned was that a second collision destroys it without doing anything else, though that part fails and it actually plays the sound again.

     void OnTriggerEnter(Collider c) 
     {
     
         if (c.gameObject.tag == "Player" && firstCol) 
         {
             Debug.Log("firstCol=" + firstCol);
             audio.PlayOneShot(gong, 1.8f);
             Player.GetComponent<Player>().Clarity += 1;
             renderer.enabled = false;
             firstCol = false;
             Debug.Log("firstCol=" + firstCol);
         }
 
         if (c.gameObject.tag == "Player" && !firstCol)
         {
             Debug.Log("firstCol=" + firstCol);
             Debug.Log("PreDestro");
             WaitForDestro();
             // tried "Destroy(gameObject);" here too, didn't work
         }
     }
 
 
     private IEnumerator WaitForDestro() {
         yield return new WaitForSeconds(0.1f);
         Debug.Log("WaitForDestro Done");
         Destroy (gameObject);
     }

firstCol is initialized as true since the first Collision is supposed to run the first if-statement. The Delay through the coroutine is there because otherwise it seems to instantly destroy the object, giving me the "Can't play a disabled audio source" problem.

as for the firstCol Debug-log, what I get there is:

 firstCol=true
 fristCol=false
 fristCol=false
 ProDestro

from walking through it once. walking through again gives me

 fristCol=false
 ProDestro

nothing else, the "WaitforDestro Done" doesn't appear either.

I also tried to work with OnTriggerExit, but that didn't solve the problem as well. Anyone know what's wrong with my Code/Approach and if there's a better way?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Jun 13, 2014 at 08:23 AM

Your problem is that firstCol is being set to true in the first if, and the second if executes immediately after the first. To solve I would suggest using an if-elseif configuration. That way the second code block will not execute if the first does.

A better way to do it would be to just use timed object destruction in the first if.

 Destroy(gameObject, 5);
 // gameObject will be destroyed in 5 seconds.

Make sure the float is longer then your sound. Also ensure you have disabled any other components of relevance on the script to prevent further collisions ect.

Edit: Further Code Sample developed in comments.

 void OnTriggerEnter(Collider c){
     if (c.gameObject.tag == "Player"){
         audio.PlayOneShot(gong, 1.8f);
         // You should consider a static reference to the player component
         Player.GetComponent<Player>().Clarity += 1; 
         render.enabled = false;
         collider.enabled = false;
         // Any other component of significance should also be disabled.
         Destroy (gameObject, 5);
     }
 }


Comment
Add comment · Show 4 · 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
avatar image Saiura · Jun 13, 2014 at 08:36 AM 0
Share

The time destruction works so far, though it now oddly enough adds 2 to the counter ins$$anonymous$$d on the first collision of just one. Plus, the object is still active during the delay between collision and destruction, allowing further collissions that increase the counter by 1 again. To solve that I'm trying to get a mix between the timed destruction and an if-elseif or rather just if-statements:

     void OnTriggerEnter(Collider c) 
     {
     
         if (c.gameObject.tag == "Player" && firstCol)
         {
             audio.PlayOneShot(gong, 1.8f);
             Player.GetComponent<Player>().Clarity += 1;
             renderer.enabled = false;
             firstCol = false;
             Destroy(gameObject, 5f);
         }
         if (c.gameObject.tag == "Player" && !firstCol)
         {
             Debug.Log("PreDestro");
             return;
         }
     }

I don't know why, but it still keeps letting me get more and more increase on the counter... (the destruction time is that high for testing reasons, I want to make sure it only lets me get one counter-increase out of a single object, no matter if it increases by one or two.

Oddly enough, the Debug.Log entry is reached every time I walk through the object, yet it still increases the counter. If I change it to an if-elseif, the first time doesn't throw the Debug-Log entry, but every other collision does... and all increase the counter none the less.

avatar image Kiwasi · Jun 13, 2014 at 08:43 AM 0
Share

I think you missed my point. Try this code.

 void OnTriggerEnter(Collider c){
     if (c.gameObject.tag == "Player"){
         audio.PlayOneShot(gong, 1.8f);
         // You should consider a static reference to the player component
         Player.GetComponent<Player>().Clarity += 1; 
         render.enabled = false;
         collider.enabled = false;
         // Any other component of significance should also be disabled.
         Destroy (gameObject, 5);
     }
 }


avatar image Saiura · Jun 13, 2014 at 08:56 AM 0
Share

oh yeah, sorry... I kinda missed the last two lines of your answer. Anyway, that works just fine now =) thanks a lot!

That would definitly be worth an upvote if I had enough reputation to do that yet xD

avatar image Kiwasi · Jun 13, 2014 at 08:56 AM 0
Share

All good. Since I've written it I will add the code to my answer.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Collecting objects? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How come when the player runs into the coin it won't destroy? 1 Answer

Is there anyway to make an object impenetrable? 1 Answer

Animator.Rebind() not working as expected after manually changing GameObject hierarchy 0 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