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 /
  • Help Room /
avatar image
1
Question by DominikHeyn · Sep 01, 2016 at 06:14 PM · waitforsecondswait

How do make an object appear and disappear after t seconds?

I want to make an object appear on collision, wait for a couple of seconds and make it disappear again.

 public class TriggerObject : MonoBehaviour {

     public GameObject object;
 
     void OnTriggerEnter(Collider other)
     {
         object.SetActive(true);
         // Wait for a second
         object.SetActive(false);
 
     }
 }

I have no idea how to make this to wait for a second... I didn't figure out how to build in "yield WaitForSeconds(1);". Is there any function which can solve my problem or is there any other way to solve this?

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

Answer by doublemax · Sep 01, 2016 at 06:42 PM

You can only use WaitForSeconds() inside a Coroutine.

  void OnTriggerEnter(Collider other)
  {
    StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second
  }

  IEnumerator ShowAndHide( GameObject go, float delay )
  {
    go.SetActive(true);
    yield return new WaitForSeconds(delay);
    go.SetActive(false);
  }
Comment
Add comment · Show 10 · 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 DominikHeyn · Sep 02, 2016 at 04:10 PM 1
Share

Works fine! Thank you very much!

avatar image NEGATIVERAGDOLL · Dec 25, 2017 at 02:22 PM 0
Share

Really sorry to bring a dead thread to life but is there a way that you can add a part to that script so the object only appears once?

avatar image JerryC92 NEGATIVERAGDOLL · May 30, 2018 at 08:15 AM 0
Share

I don't know if you ever figured something out, but maybe you could try to include a counter. This way, you can limit the amount of times the object appears (i.e. counter = 0, object appears, then disappears, counter goes up from 0 to 1. If (counter <= 1), process does not repeat. Just a general idea.

avatar image TheCyborne · Jun 03, 2019 at 08:50 PM 0
Share

i got a error $$anonymous$$essage When putting it in Here is my Code i attached a screenshot of the errors i'm getting also alt text

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

public class TriggerObject : $$anonymous$$onoBehaviour

  public GameObject object;
 
 void OnTriggerEnter(Collider other)

{ StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second } IEnumerator ShowAndHide( GameObject go, float delay ) { go.SetActive(true); yield return new WaitForSeconds(delay); go.SetActive(false); }

untitled.png (47.1 kB)
avatar image highpockets TheCyborne · Jun 03, 2019 at 09:32 PM 0
Share

@TheCyborne You need to put “{“ after “$$anonymous$$onoBehaviour” and “}” at the end of the script. Gotta remember those parentheses

avatar image TheCyborne · Jun 04, 2019 at 12:42 AM 0
Share

@highpockets i got another error After i fixed my last error and forgive me i'm new to coding and Unity Here is my Code

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

public class TriggerObject : $$anonymous$$onoBehaviour {

 public GameObject object;
 
 
  void OnTriggerEnter(Collider other)

{ StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second } IEnumerator ShowAndHide( GameObject go, float delay ) { go.SetActive(true); yield return new WaitForSeconds(delay); go.SetActive(false);

}

alt text

avatar image highpockets TheCyborne · Jun 04, 2019 at 05:39 AM 0
Share

Ok, you said you’re getting an error, but didn’t mention what it said. Hard to help, but I believe it is because you are missing an additional parentheses at the end of the script “}” , you are also posting most of your script as regular text which doesn’t help. Format the code and use the script button

avatar image TheCyborne · Jun 04, 2019 at 06:42 AM 0
Share

@highpockets Here is my Code Again i'm Sorry about the Question `

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
  public class TriggerObject : $$anonymous$$onoBehaviour {
      
            void OnTriggerEnter(Collider other)
       {
         StartCoroutine( ShowAndHide(Star, 1.0f) ); // 1 second
       }
       IEnumerator ShowAndHide( GameObject go, float delay )
       {
         go.SetActive(true);
         yield return new WaitForSeconds(delay);
         go.SetActive(false);
       }
avatar image highpockets TheCyborne · Jun 04, 2019 at 07:18 AM 0
Share

Sorry, but did you read my last comment? You are missing the last parentheses. You need to put this:

 }

At the very end of your code

avatar image TheCyborne highpockets · Jun 04, 2019 at 07:12 PM 0
Share

@highpockets thank you for the help Sorry i somehow read over it but thanks for the help

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

71 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

Related Questions

reload script assemblies is VERY Slow. 1 Answer

Noobie question: wait a random time 1 Answer

How to use IEnumerator? Pls help! 1 Answer

Wait in unity(coroutine not working) 0 Answers

How to delay an action in Untiy 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