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 Timvb_97 · Jul 03, 2020 at 12:44 PM · objectontriggerenterdelayienumeratorstartcoroutine

How to appear object on trigger delay?

Hi! I have a question and I hope somebody could help me further. So, I want a object in my game to appear after 7 seconds when my player has entered a specific area. I think it's something with OnTriggerEnter, but how do I put this in my script? At the moment, this is my script.

public class AppearSeconds : MonoBehaviour

{ public GameObject Object;

 void Start()
 {
     StartCoroutine(ShowAndHide(7.0f)); 
 }

 IEnumerator ShowAndHide(float delay)
 {
     Object.SetActive(false);
     yield return new WaitForSeconds(delay);
     Object.SetActive(true);
 }

}

When I start the game, after 7 seconds the object appears, so the delay works. But i want the counter to start running only after the player triggers with another object (area in the game). The area is called 'CubeScene2' and the object that I want do appear after 7 seconds is 'Icoontje'. Right now, the script is connected to 'CubeScene2'. As you can see, there is a public game object in the script. I place the object 'Icoontje' in this public game object. Can somebody help me?

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
Best Answer

Answer by Beylan0 · Jul 03, 2020 at 01:03 PM

You could check if your object enters a specific trigger by assigning a new layer for that specific object (9 is the index value of your trigger object's layer):

         private void OnTriggerEnter(Collider collision)
         {
             if (collision.gameObject.layer == 9)
             {
                 StartCoroutine(ShowAndHide(7.0f));
             }
         }
         IEnumerator ShowAndHide(float delay)
         {
             Object.SetActive(false);
             yield return new WaitForSeconds(delay);
             Object.SetActive(true);
         }

@Timvb_97

Comment
Add comment · Show 3 · 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 Timvb_97 · Jul 04, 2020 at 05:44 PM 0
Share

Thank you for your answer! It does work now, but not exactly how I want it to. If I start the game and put the player directly in to the trigger area (the transparent cube object, connected with this script) then the object 'Icoontje' appears after 7 seconds, as it should be. But when I don't put the player directly in to the area (the invisible cube is a bit further away), you will see the object 'Icoontje' directly.

I think I need something to do with Object.SetActive false and only set to true when the player collides with the area (transparent cube), but how? Note, I'm a bit new to Unity, therefore the questions haha.

avatar image Beylan0 Timvb_97 · Jul 04, 2020 at 07:06 PM 0
Share

If you don't want your object to be shown at the beginning, then you could turn this off:

Image

And because you already disabled the object,

 Object.SetActive(false);

This code will be useless, so you can remove it. @Timvb_97

avatar image Timvb_97 Beylan0 · Jul 06, 2020 at 11:49 AM 0
Share

Thank you, thank you, thank you! Legend! It works now :)

avatar image
0

Answer by Timvb_97 · Jul 06, 2020 at 12:42 PM

Hi @Beylan0 I have another question, maybe you also know what I need to do. In the same game, you hear a voice over. Right now, the voice overs (different audio sources) are connected to the player. Example, the player walks to an empty bottle and grabs it (VR), then you will hear a voice over "Hmm, empty, I need to refill this". This bottle is connected to a script with 2 public audio sources and a void OnTriggerEnter, as follows public class TriggerSFX : MonoBehaviour { public AudioSource playSound; public AudioSource playSound2;

  void OnTriggerEnter(Collider other)
  {
      playSound.Play();
      playSound2.Play();
  }

} The audio source with the line "Hmm, empty, I need to refill this" is connected to the player. This audio source will be put in the public audio source playSound. This works, but not as I want it to do. I only want the sound to be heard just 1 time. Right now, you will hear this sound everytime you pick the bottle up. And further, you see 2 audio sources and 2 triggers. This is because in some cases, I want 2 voice overs on one trigger but the second voice over a bit later, couple seconds, with delay. I've searched the whole internet to find the answers for both questions and tried many things, but I just couldn't figure it out. Can you help me with this?

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

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

144 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 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

issue with making a moving object trap c# (SOLVED) 2 Answers

How to make object clickable when user enter collider ? 0 Answers

Delay between clicks and sound 2 Answers

Instantiation Wont Stop 1 Answer

Item duration doesn't stack 3 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