Can you help me with ''yield WaitForSeconds''?
Hello everyone! Can you help me? I was trying to add enemy AI and when enemy catches you you get jumpscared by it.I made it but after jumpscare i wanted to add "yield WaitForSeconds'' and ''Application.LoadLevel''... but it didn't work... Level didn't want to load! Could you write script for me that changes scene after some time? Thanks :)
Answer by HenryStrattonFW · Feb 09, 2017 at 08:00 PM
It would help to actually show us the code you're currently using as this may effect the kind of response you will get, and the best approach to take. But if we ignore any existing code, this is a Coroutine that would delay a scene load based on a delayTime (in seconds).
private IEnumerator _DelayedLoad(float delayTime, string sceneToLoad)
{
yield return new WaitForSeconds(delayTime);
SceneManager.LoadScene(sceneToLoad);
}
You just then call this with StartCoroutine and it should do the job. so for example.
public void GetJumpScared()
{
// Other jump scare stuffs.
StartCoroutine(_DelayedLoad(2.0f, "MyScene"));
}
I get so many errors xD could you explain me where should i put those things? xD btw I already know that I have to add ''using UnityEngine.Scene$$anonymous$$anagement;
I'm getting "error CS1525: Unexpected symbol string', expecting
,', ;', or
='" and something like unexpected symbol "public" and sometimes "private".... help xD
sorry for taking your time
Ah I should have mentioned, the script I provided was in C# are you using C# scripts or javascript? If using javascript this code whilst accurate in its process, will not be correct syntax which might explain the errors.
If however you are using C# can you please past the script you are currently using so that I can see it in it's entirety in order to try and work out what's going wrong.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class deathmenu : $$anonymous$$onoBehaviour {
private IEnumerator _DelayedLoad(float delayTime, string deathmenu)
{
void OnTriggerEnter(Collider theCollision)
{
if (theCollision.gameObject.tag == "Player")
yield return new WaitForSeconds(3);
Scene$$anonymous$$anager.LoadScene(deathmenu);
}
public void GetJumpScared
{
StartCoroutine(_DelayedLoad(2.0f,"H2"));
}
}
I deleted the code so I wrote it from my memory xD
Your answer
Follow this Question
Related Questions
Can OnTriggerEnter2D trigger again once triggered? 0 Answers
OnTriggerEnter2D() function not working 1 Answer
Input Field URL browser. Please help! 0 Answers
Spawning with trigger 2 Answers
How to identify which collider generated OnTriggerEnter2D 0 Answers