- Home /
Destroy a game object but I would like to have it return?
When my player touches and object is triggered runs and I use.
Destroy(other.gameObject)
This Destroys the object. Is there another way to make this object disappear so it doesn't effect my player movement and have it return after a few seconds or something like that?
Answer by Derek-Wong · Jul 11, 2017 at 04:55 AM
You may try:
yourGameObject.setActive(false)
And when u need it back:
yourGameObject.setActive(true)
Answer by Cornelis-de-Jager · Jul 11, 2017 at 05:22 AM
Like the others said, you can just use setActive, but if you want it to be activated after a while you can do the following:
void OnCollisionEnter (Collision other) {
// Disable
other.gameObject.setActive (false);
// Re-enable
StartCoroutine (SetActive (other, 5f));// set active after 5 seconds
}
Enumarator SetActive (Collision other, float seconds) {
// Wait for the specified time
yield return new WaitForSeconds (seconds);
// Set the object enabled again
other.gameObject.setActive (true);
}
I dont understand how the seconds is working or how to implement it back in my code
@sreichen, seconds is simply how long you want to wait until the object respawns.
Answer by HarshadK · Jul 11, 2017 at 04:54 AM
You can use GameObject.SetActive to deactivate your gameobject rather than destroying it. Then you can activate the gameobject again to get it back.
Your answer
Follow this Question
Related Questions
Did triggers inside loaded gameobjects break in a recent update? 0 Answers
Bouncing the ball off a collider and destroying it using trigger 0 Answers
Checking if colliding with anything in a layer 1 Answer
How to single out specific colliders from another object 1 Answer
How do I prevent multiple triggers using OnTriggerEnter? 1 Answer