- Home /
How to load a level after 30 seconds?
Hello. I am making a game that is intended for the Android and is 2D. I can't figure out how to load a level after 30 seconds. The game is supposed to last only 30 seconds and then is suppose to take you to a different scene ( Level ).
The script I am using is
using UnityEngine;
using System.Collections;
public class Clock : MonoBehaviour {
int hours;
public int hours24;
public float minutes;
public int iMinutes;
string fMinutes;
string ampm;
TextMesh clockText;
void Start () {
clockText = GetComponent<TextMesh> ();
hours = 12;
hours24 = 12;
minutes = 01;
ampm = "pm";
}
void Update () {
// Build Time. Default is one minute per second.
minutes += Time.deltaTime;
iMinutes = (int) minutes ;
fMinutes = iMinutes.ToString("D2");
if (iMinutes >= 60)
{
if (hours == 12)
{
hours = 0;
}
if (hours == 11)
{
if (ampm == "am")
ampm = "pm";
else
ampm = "am";
}
if (hours24 == 23)
hours24 = 0;
hours += 1;
hours24 += 1;
iMinutes = 0;
minutes = 0;
}
clockText.text = fMinutes ;
}
}
The counter works with Iminutes which are called minutes but count in seconds.
How can I modify the script that when the Iminutes reaches 30 a different scene loads.
All help is appreciated. If there are any questions, please ask.
That wasn't what I meant, thanks for the responses although. I mean what do I write in order for it know for it to be done at 30 seconds.
why dont you use StartCoroutine; yield return new waitforsecond(30f);
Answer by Priyanshu · Mar 21, 2015 at 06:13 AM
If you are having such a problem:
You need to learn about conditional statements in c#.
You can learn to use Application.LoadLevel here.
To modify the script that, when the Iminutes reaches 30 a different scene loads.
You need to add the following 'If' statement inside Update() in the end :
if( iMinutes >= 30)
{
//You need to pass Level index or Level name in the method.
Application.LoadLevel(x);
}
Now,
Press ctrl+shift+b, if working in windows to open the Build Settings window.
Add your scenes there. By pressing the Add Current button or drag and dropping scenes from Project window into Build Settings Window.
Note the name or Integer value infront of the scene name.
Replace 'x' in Application.LoadLevel(x); with it.
For example: replace 'x' with "Test Scene" or simple "0".
Application.LoadLevel(Test Scene); Or Application.LoadLevel(0);
Alright, I did as you have said ( Hopefully ), but I am getting these errors. ( Here is the Script)
using UnityEngine;
using System.Collections;
public class Clock : $$anonymous$$onoBehaviour {
int hours;
public int hours24;
public float $$anonymous$$utes;
public int i$$anonymous$$inutes;
string f$$anonymous$$inutes;
string ampm;
Text$$anonymous$$esh clockText;
void Start () {
clockText = GetComponent<Text$$anonymous$$esh> ();
hours = 12;
hours24 = 12;
$$anonymous$$utes = 01;
ampm = "pm";
}
void Update () {
// Build Time. Default is one $$anonymous$$ute per second.
$$anonymous$$utes += Time.deltaTime;
i$$anonymous$$inutes = (int) $$anonymous$$utes ;
f$$anonymous$$inutes = i$$anonymous$$inutes.ToString("D2");
if (i$$anonymous$$inutes >= 60)
{
if (hours == 12)
{
hours = 0;
}
if (hours == 11)
{
if (ampm == "am")
ampm = "pm";
else
ampm = "am";
}
if (hours24 == 23)
hours24 = 0;
hours += 1;
hours24 += 1;
i$$anonymous$$inutes = 0;
$$anonymous$$utes = 0;
}
clockText.text = f$$anonymous$$inutes ;
}
if ( i$$anonymous$$inutes >= 30)
{
//You need to pass Level index or Level name in the method.
Application.loadedLevel();
}
}
Here are the errors
Assets/Standard Assets ($$anonymous$$obile)/Textures/Clock.cs(61,1): error CS8025: Parsing error
Assets/Standard Assets ($$anonymous$$obile)/Textures/Clock.cs(59,40): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/Standard Assets ($$anonymous$$obile)/Textures/Clock.cs(56,24): error CS1519: Unexpected symbol
>=' in class, struct, or interface member declaration
Assets/Standard Assets ($$anonymous$$obile)/Textures/Clock.cs(56,10): error CS1519: Unexpected symbol `if' in class, struct, or interface member declaration
sorry i used "Application.loadedLevel" ins$$anonymous$$d of "Application.LoadLevel".
And you get that error because you wrote that code outside Update(). Write it just below clockText.text = f$$anonymous$$inutes ;
You need to add scene in build setting and pass a value inside "Application.LoadLevel()"
Edited my answer
Answer by Ghopper21 · Mar 21, 2015 at 04:54 AM
When the time reaches 30 seconds, execute Application.LoadLevel()
Answer by Johnz1234 · Mar 21, 2015 at 10:40 AM
#pragma strict
var time : float = 0;
function Start() {
time = 5;
}
function Update () {
if(time > 0){
time-=Time.deltaTime;
}else{
Application.LoadLevel("Level1");
}
}
Your answer
Follow this Question
Related Questions
Why isn't this working? 3 Answers
Game looks different on device compared to Game View 0 Answers
Application.Loadlevel("Level Name"); not working on android 2 Answers
OpenIAB - billing plugin. It works only once! need HELP! 1 Answer
Game freeze while I click Gui texture loadlevel to change scene 0 Answers