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!
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.
Your answer
Follow this Question
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