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 kim nana · Oct 23, 2014 at 06:38 PM · c#time

How to delay time before pop-up new game object

Hello. I really need help because currently I am doing on this task for my project. How to delay time (seconds) right after the game object has been destroyed? When the game object is destroyed, a new game object will be appeared on the same window. Below is my code:

 void OnCollisionEnter (Collision collision)
 {            
     if (collision.relativeVelocity.magnitude>5)
     {    
         AnswerTrue.DestroyAddScore();
         Destroy(gameObject); 
         Instantiate(SpawnParticle,transform.position,Quaternion.identity); //particles appear when the gameobject is destroyed
         score+=2; // the score is added when the gameobject is destroyed
         Destroy(gameObject);
         EndTrue.SetActive(true); // the new gameobject will be appeared as its new window
     }
 }

I tried to use "IEnumerator" but it does not work very well. The object is destroyed & the score is counted but the new game object does not appear as its new window. Below shows the code that I had edited.

 IEnumerator OnCollisionEnter (Collision collision)
 {            
     if (collision.relativeVelocity.magnitude>5)
     {    
         AnswerTrue.DestroyAddScore();
         Destroy(gameObject); 
         yield return new WaitForSeconds(2); // time delay for 2 seconds
         Destroy(gameObject);
         Instantiate(SpawnParticle,transform.position,Quaternion.identity); //particles appear when the gameobject is destroyed
         score+=2; // the score is added when the gameobject is destroyed
         Destroy(gameObject);
         EndTrue.SetActive(true); // the new gameobject will be appeared as its new window
     }
 }


I really hope that my question is clear now. I really need your help & thank you so much for your attention.

Comment
Add comment · Show 4
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 Ed unity · Oct 23, 2014 at 09:52 PM 0
Share

The function should still complete and Instantiate the object because the Destroy wont happen right away, so I believe that there is an issue with your Instantiate call and not with the ti$$anonymous$$g. You should try instantiating the object in start or update without destroying the object that instantiates it. Just to make sure that your instantiate call is actually working properly. Then move it to the Collision Function.

avatar image UnityCoach · Oct 23, 2014 at 10:02 PM 0
Share

huh, you destroy the gameObject you're sitting on .. destroy a game object will cancel all its coroutines...

avatar image kim nana · Oct 24, 2014 at 02:43 AM 0
Share

Okay, I already remove that "destroy game object". And I try to put that instantiating in "start" or "update" but still can't work properly. The first coding that I've give is works but the problem is I don't know how to delay the time. So I think it most probably problem on the instantiate part.

avatar image imking · Oct 24, 2014 at 06:30 AM 0
Share

You can use OnDestoy() in $$anonymous$$onoBehaviour. When your gameObject destored, call the OnDestory().

Your code add in OnDetroy()

Reference Description

$$anonymous$$onoBehaviour.OnDestroy() This function is called when the $$anonymous$$onoBehaviour will be destroyed.

1 Reply

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

Answer by Kiwasi · Oct 24, 2014 at 03:07 AM

  IEnumerator OnCollisionEnter (Collision collision)
  {            
      if (collision.relativeVelocity.magnitude>5)
      {    
          AnswerTrue.DestroyAddScore();
          renderer.enabled = false; 
          yield return new WaitForSeconds(2); // time delay for 2 seconds
          Instantiate(SpawnParticle,transform.position,Quaternion.identity);
          score+=2;
          EndTrue.SetActive(true); 
          Destroy(gameObject);
      }
  }


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 kim nana · Oct 24, 2014 at 09:58 AM 0
Share

I've tried to use your code. And it was awesome! I already got what actually I want. Thank you sooo much. :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fade in AudioLister over time #c 1 Answer

Translating UnityScript to C#. time.delta displays differently 2 Answers

2D Jagged Array , Array index is out of range. 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