- Home /
Duplicate Question
C# Load Level after seconds
Here is another C# issue that seemed to be easier in java, my code guy likes C# so I have been trying to save him a lot of time converting what I can to C#, I have tried looking this up but I keep finding these long over complicated methods to do something I believe should be quite simple. Anyways all I basically want to do is ad a slight delay in my C# script so the level will load after a short second or so. This is my very simple next level load code which works, just works too fast ;)
using UnityEngine;
using System.Collections;
public class SCBlueButton : MonoBehaviour {
public void ModeSelect(){
Application.LoadLevel("SurveillanceModeSelectScreen");
}
}
I do believe there is a simple solution to your problem. I don't know if you still try to solve it in a more simple way, but here is my solution for this:
> > using UnityEngine; using
> > System.Collections; public class
> > SCBlueButton : $$anonymous$$onoBehaviour {
> >
> > private float waitForSeconds;
> >
> > void Start(){
> >
> > Invoke("$$anonymous$$odeSelect", waitForSeconds);
> >
> > }
> >
> > public void $$anonymous$$odeSelect(){
> > Application.LoadLevel("Surveillance$$anonymous$$odeSelectScreen");
> > } }
Answer by shriya · Jan 23, 2015 at 12:29 PM
hi
Use below mentioned code
using UnityEngine;
using System.Collections;
public class SCBlueButton : MonoBehaviour {
public void ModeSelect(){
StartCoroutine("Wait");
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2);
Application.LoadLevel("SurveillanceModeSelectScreen");
}
}
2 can be changed to any value i.e the specific amount of time you want to wait before loading level.
This code will still load the level immediately. The line
Application.LoadLevel("Surveillance$$anonymous$$odeSelectScreen");
should be inside coroutine below yield statement.
using UnityEngine;
using System.Collections;
public class SCBlueButton : $$anonymous$$onoBehaviour {
public void $$anonymous$$odeSelect(){
StartCoroutine("Wait");
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2);
Application.LoadLevel("Surveillance$$anonymous$$odeSelectScreen");
}
}
I ended up figuring it out after some additional googling but yup, that was it. $$anonymous$$ine looks a tiny bit different but the idea is basically the same, thanks Harshad$$anonymous$$ :)
using UnityEngine;
using System.Collections;
public class SCBlueButton : $$anonymous$$onoBehaviour {
public void $$anonymous$$odeSelect(){
StartCoroutine(LoadAfterDelay("Surveillance$$anonymous$$odeSelectScreen"));
}
IEnumerator LoadAfterDelay(string levelName){
yield return new WaitForSeconds(01); // wait 1 seconds
Application.LoadLevel(levelName);
}
}
I am trying to get a video to play, so like remember if you were idle at a NES game for about a $$anonymous$$ute then a video story would run. I have an object with a movie material in the scene and after some time I'd like it to change to active (overtake camera view< I got that) and then start playing.
public void Play$$anonymous$$ovie()
{
StartCoroutine("Wait");
}
IEnumerator Wait()
{
yield return new WaitForSeconds (01f);
movie.SetActive (true);
GetComponent<Renderer> ().material.mainTexture = sS$$anonymous$$ain$$anonymous$$enu;
sS$$anonymous$$ain$$anonymous$$enu.Play ();
the object with the movie material is set to "not active" by default. I got this to work with a pause menu, but trying this with a movie on a timer isnt' working lol. Any ideas?
Answer by ramp · Jan 23, 2015 at 12:34 PM
Hello,
In C#,you can use code like this.
IEnumerator LoadAfterWait(string levelName)
{
yield return new WaitForSeconds(2f); // wait 2 seconds
Application.LoadLevel(levelName);
}
And you must start the coroutine like this:
StartCoroutine(LoadAfterWait("SurveillanceModeSelectScreen"));
Thanks
Ram
Thanks, I suppose my only other question would be for how to make the time about half a second?
hi you shriya . tank you so much. cz your code work in my project.
Follow this Question
Related Questions
WaitForSeconds load level delay not working 1 Answer
Csharp: how to make script wait for x seconds 3 Answers
Run script when level begins? 1 Answer