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 /
  • Help Room /
avatar image
0
Question by BNOCSK · Oct 19, 2021 at 11:48 AM · coroutineactiveinsantiate

How do I respawn a gameobject after time

Hi all,

I'm having difficulty respawning my gameobject after a given time and I'm just looking for some pointers/help if possible.

It's actually a node (ore) and the stages are:

initial node (Initial GameObject shows) - User actions Active node hides Depleted (Separate GameObject shows) Wait X seconds Depleted node is destroyed and the initial GameObject respawns (with reset functionality if possible)

 public void MineResource()
         {
             if (resourcesRemaining == 0)
             {
                 Debug.Log("Hiding original");
                 gameObject.SetActive(false);
 
                 Debug.Log("Instantiating Depleted object");
                 GameObject depletedObject = Instantiate(depletedPrefab, transform.position, depletedPrefab.transform.rotation);
                 depletedObject.SetActive(true); // haven't checked this is required after instantiate
 
                 // At this point, the original is hidden and the second is showing
                 // here I'm trying to wait 5 seconds and respawn the original.
                 StartCoroutine(ResetMineable(gameObject, depletedObject, 5f));
             }
         }
 
         public IEnumerator ResetMineable(GameObject gameObject, GameObject depletedObject, float duration)
         {
             // I only want to hide the depletedObject after the wait time
            // as well as respawn the original gameObject
              
             Debug.Log("Hiding Depleted");
             depletedObject.SetActive(false);
 
             Debug.Log("Trying to instantiate original");
             gameObject.SetActive(true);
 
             Debug.Log("Waiting duration");
             yield return new WaitForSeconds(duration);
         }

Hopefully that makes sense with what I'm trying to do but please ask if not.

Basically, I've shuffled it around and tried different things but to no avail - In the Coroutine I ideally want to wait the timer out before I remove the depletedObject and set the original to active again.

As a side note, I've also tried various ways using the below to just hide it, but you can still action it as if it's there.

  gameObject.GetComponent<MeshRenderer>().enabled = false;

The script is attached to the original gameObject.

Any help will be appreciated, thanks!

Comment
Add comment · Show 1
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 BNOCSK · Oct 19, 2021 at 01:56 PM 0
Share

EDIT:

I have gotten this to work now using the code below:

 public void MineResource()
     {
         if (resourcesRemaining == 0)
         {
             gameObject.GetComponent<MeshRenderer>().enabled = false;
             // Script that allows it to be actionable 
             gameObject.GetComponent<Mineable>().enabled = false;
     
             Debug.Log("Instantiating Depleted");
             GameObject depletedObject = Instantiate(depletedPrefab, transform.position, depletedPrefab.transform.rotation);
     
             StartCoroutine(ResetMineable(depletedObject, gameObject, 5f));
             //onEmpty?.Invoke();
         }
     }
     
     public IEnumerator ResetMineable(GameObject depletedObject, GameObject originalObject, float duration)
     {
         yield return new WaitForSeconds(duration);
     
         Debug.Log("Remove Depleted");
         Destroy(depletedObject);
     
         Debug.Log("Show original and reset");
         originalObject.GetComponent<MeshRenderer>().enabled = true;
         originalObject.GetComponent<Mineable>().enabled = true;
         Mineable component = originalObject.GetComponent<Mineable>();
         component.resourcesRemaining = 5;
     }

Can ignore the variable na$$anonymous$$g as I was trying everything so need a little tidy but it works!

Now my question is slightly different - Is this the best way of doing this?

This is purely a test/mess around project away from my day to day non game related development but always appreciate improvement and the opportunity to learn.

Thanks.

0 Replies

· Add your reply
  • Sort: 

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

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

Persistent Timers in Couroutines 0 Answers

Issues with video player coroutine 2 Answers

Co-routine a recursion help 0 Answers

Create events that fires off a coroutine from another script 0 Answers

Coroutine makes unity to freeze till it is done 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