Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ughdeeb · Nov 26, 2018 at 08:57 PM · canvasontriggerenteraudioclipsetactive

Need UI Canvas to appear AFTER audio finishes on trigger

Hi there,

I have a Sound Trigger set up so that when the player enters the audio will play once. What I cant figure out is how to make a canvas I have with buttons appear AFTER the audio finishes. The canvas has buttons that when selected will trigger more audio and I do not want the user to press the button until the triggered audio completes. Below is the code I have so far:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class AudioTrigger : MonoBehaviour { public AudioClip audclip; public bool havePlayed = false; void Start()

 {
     havePlayed = false;
     GetComponent<AudioSource>().playOnAwake = false;
     GetComponent<AudioSource>().clip = audclip;
 }

 void OnTriggerEnter(Collider whoCollidedWithMe)       //Plays Sound whenever collision detected
 {
     if (!havePlayed)    // !havePlayed is the same as havePlayed==false
     {
         // we haven't played yet, but someone collided with us. Who is it ?
         if (whoCollidedWithMe.gameObject.tag == "Player")
         {
             GetComponent<AudioSource>().PlayOneShot(audclip);
             Debug.LogWarning("are u working");
             havePlayed = true;  // we played, so never again
         }
         else
         {
             // not the player, so do nothing
         }

     }
    
 }
 

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Nov 26, 2018 at 09:24 PM

What you could do is Invoke a method with the length of the audio clip, so something like this:


             Invoke("EnableCanvas", audclip.length);



The invoke will call a method named EnableCanvas (that deals with enabling it) with a delay for as long as the clip. Just put this under your playOneShot(audclip);


It could also be a coroutine - StartCoroutine(EnableCanvas(audclip.length);

Then have the yield the length of the clip. yield return newWaitForSeconds(length);

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

Answer by ughdeeb · Nov 26, 2018 at 09:45 PM

Hi @Vega4Life! Thanks for your help.

I am really new to coding, could you provide more detail for the Coroutine route? Not sure where to put that in the script :/

Thanks

Comment
Add comment · Show 1 · 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 Vega4Life · Nov 27, 2018 at 02:01 AM 0
Share

Here is something quick and dirty. It uses a coroutine with a reference to the canvas. Everything with //** is what I added:


     //** Reference your Canvas
     [SerializeField] Canvas uiCanvas;
 
     void OnTriggerEnter(Collider whoCollidedWith$$anonymous$$e)       //Plays Sound whenever collision detected
     {
         if (!havePlayed)    // !havePlayed is the same as havePlayed==false
         {
             // we haven't played yet, but someone collided with us. Who is it ?
             if (whoCollidedWith$$anonymous$$e.gameObject.tag == "Player")
             {
                 GetComponent<AudioSource>().PlayOneShot(audclip);
 
                 //** Send clip timer to coroutine \\**
                 StartCoroutine(EnableCanvas(audclip.length));
                 uiCanvas.enabled = false;
                 // ** \\
 
                 Debug.LogWarning("are u working");
                 havePlayed = true;  // we played, so never again
             }
             else
             {
                 // not the player, so do nothing
             }
         }
 
     }
 
     //** Coroutine that will wait x amount of timer before executing
     IEnumerator EnableCanvas(float timer)
     {
         yield return new WaitForSeconds(timer);
 
         uiCanvas.enabled = true;
     }

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

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

Insane amount of CPU power used to activate UI inventory window 2 Answers

physics.OverlapSphere colliders 1 Answer

Program control not entering onTriggerEnter() 1 Answer

Detect OnTriggerEnter with tags 0 Answers

Strange error with UI Screen overlay - no bugs or warning, game can't be played. - GameOverText.SetActive (false); 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