- Home /
 
Jumpscare Need Help
Hello everyone. I am new to Unity and I need a hand with a script for a jumpscare. I have attempted it and put all the triggers etc. Here is my attempt that I try but all I get are errors
 public GameObject jumpscareObject;
 void Start () {
     jumpscareObject.SetActive(false);
 }
 void OnTriggerEnter (Collider player) {
     if(player.tag == "Player")
     {
         jumpscareObject.SetActive(true);
        
     yield return new WaitForSeconds(1.5f);
     Destroy(jumpscareObject);
     Destroy(gameObject);
 }
 
               }
Well, maybe you should start by telling us what errors you are receiving?
And how your script is setup in your game scene.
also yield return new WaitForSeconds(1.5f); does not work unless called as a coroutine. 
Very difficult to help when you don't tell:
what exactly you want the code to do
what it is doing wrong now - what is the problem you are having
what are the errors you say you are getting
Answer by Shippety · Jul 08, 2017 at 07:05 PM
Like @ShadyProductions said, you need to use a coroutine if you want to use the delay. I would reorganize what you have above like this:
 public GameObject jumpscareObject;
 
 void Start()
 {
     jumpscareObject.SetActive(false);
 }
 void OnTriggerEnter(Collider player)
 {
     if (player.tag == "Player")
     {
         StartCoroutine(DoTheSpook());
     }
 }
 
 IEnumerator DoTheSpook()
 {
     jumpscareObject.SetActive(true);
     yield return new WaitForSeconds(1.5f);
     Destroy(jumpscareObject);
     Destroy(gameObject);
 }
 
               Hope that helps!
Answer by Jwizard93 · Jul 10, 2017 at 01:31 AM
 IEnumerator OnTriggerEnter (Collider player)
 {
     if(player.tag == "Player")
     {
          jumpscareObject.SetActive(true);
         
          yield return new WaitForSeconds(1.5f);
          Destroy(jumpscareObject);
          Destroy(gameObject);
 
     }
 }
 
               OnTriggerEnter can be used as a coroutine just give it the proper return type.
Your answer
 
             Follow this Question
Related Questions
Avoiding Bloom Pop 1 Answer
Enabling disabling objects via raycast 0 Answers
How to create an Animated 2D Line to help the user aim and shoot? 0 Answers
Letting the player place objcts using navmesh 0 Answers
How to make a script slow down to a stop 0 Answers