script to display an object in unity
I need a script that will show an object in my game for a few seconds and then disappear
public class disappear : MonoBehaviour
{
public int disappearAfter = 3;
void Start()
{
Invoke("visible", 0);
Invoke("invisible", disappearAfter);
}
private void visible()
{
gameObject.SetActive(true);
}
private void invisible()
{
gameObject.SetActive(false);
}
}
You could also destroy the object but then you would need to place the script on another object and reference it
public class destroy: MonoBehaviour
{
public GameObject disappearingObject;
public int disappearAfter = 3;
void Start()
{
Invoke("visible", 0);
Invoke("invisible", disappearAfter);
}
private void visible()
{
disappearingObject.SetActive(true);
}
private void invisible()
{
destroy(disappearingObject);
}
}
Hope this helps