- Home /
How to make an object reappear?
So, I'm trying to make a platform that disappears a couple seconds after the player steps on it, and then reappears after a couple more seconds.
So far, the "disappearing" part works, but I can't get the platform to reappear. Here's my script:
public class VanishScript : MonoBehaviour {
public float Magic = 4;
public float Seconds = 2;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Player")
{
StartCoroutine(Poof());
StartCoroutine(Wizard());
}
}
IEnumerator Poof()
{
yield return new WaitForSeconds(Seconds);
Destroy(gameObject);
}
IEnumerator Wizard()
{
yield return new WaitForSeconds(Magic);
gameObject.SetActive(true);
}
Answer by M-G-Production · Aug 17, 2017 at 10:31 PM
The problem is... When you destroy your gameObject, the script can no longer be executed...
Here:
IEnumerator Poof()
{
yield return new WaitForSeconds(Seconds);
Destroy(gameObject); //THIS LINE!!
}
There are different ways of doing it... One could be to affect the scale and set it to 0? Or why not disabling the renderer and collider?
I'm gonna put the exemples as comments.
Option A: Renderer and Collider:
public class VanishScript : $$anonymous$$onoBehaviour
{
public float $$anonymous$$agic = 4;
public float Seconds = 2;
Renderer myR;
Collider myC;
void OnTriggerEnter(Collider other)
{
myR = GetComponent<Renderer>();
myC = GetComponent<Collider>();
if (other.gameObject.name == "Player")
{
StartCoroutine(Poof());
StartCoroutine(Wizard());
}
}
IEnumerator Poof()
{
yield return new WaitForSeconds(Seconds);
myR.enabled = false;
myC.enabled = false;
//Destroy(gameObject);
}
IEnumerator Wizard()
{
yield return new WaitForSeconds($$anonymous$$agic);
myR.enabled = true;
myC.enabled = true;
//gameObject.SetActive(true);
}
}
Option B: Scale
public class VanishScript : $$anonymous$$onoBehaviour
{
public float $$anonymous$$agic = 4;
public float Seconds = 2;
Vector3 iniScale;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Player")
{
iniScale = transform.localScale;
StartCoroutine(Poof());
StartCoroutine(Wizard());
}
}
IEnumerator Poof()
{
yield return new WaitForSeconds(Seconds);
transform.localScale = Vector3.zero;
//Destroy(gameObject);
}
IEnumerator Wizard()
{
yield return new WaitForSeconds($$anonymous$$agic);
transform.localScale = iniScale;
//gameObject.SetActive(true);
}
}
Thank you! I'll remember this the next time I need to make something reappear.
Answer by barrettm · Aug 17, 2017 at 10:39 PM
Instead of destroying the platform gameobject in your Poof
coroutine, just call SetActive(false)
.
That was the first thing I tried, but I couldn't set it back to SetActive(true). Thank you, anyway!
Answer by unidad2pete · Aug 17, 2017 at 07:28 PM
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Player")
{
StartCoroutine(Poof());
}
}
IEnumerator Poof()
{
int phase = 0;
while(true)
{
if(phase == 0)
{
phase++;
yield return new WaitForSecondsRealtime(Seconds);
} else
{
if(phase == 1)
{
phase++;
gameObject.SetActive(false); // If script stops when object set inactive you can change the position
//gameObject.transform.position = gameObject.transform.position + Vector3.up * 1000;
yield return new WaitForSecondsRealtime(Seconds);
} else
{
gameObject.SetActive(true);
//gameObject.transform.position = gameObject.transform.position - Vector3.up * 1000;
yield break;
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Rotating Platform 0 Answers
Test game on iPhone 1 Answer