- Home /
Application.LoadLevel click problem
I am very new to Unity and am having an issue.
On my Game scene the user is tap on moving objects. When the user has touched 10 of the correct objects. My script sees the level is completed and I call Application.LoadLevel("FinishedLevel") within the Update method.
The issue is, if the last tap on the Game scene is in the same location a button is on the FinishedLevel screen, that button call is executed.
Does anyone know how to prevent this?
Can you show your code for the button appearing and how it detects the user input. The answer will be different depending on your code.
You are probably handeling it wrong. If you detect touch in the Update function. You can check if the touch phase has ended. If so, load the new level. The ending touchphase takes 1 frame. So that means it will only be called once while still being in the Update function.
I have a CS file Called CurrentLevel.cs which is bound to the scene. I then have 4 UIButtons on the screen which each reference a unique public function on the OnClick event. OnQuit is one that keeps getting pressed.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Prime31;
//this total class by techspawn solutions
// this has to be loaded at the time of when user completes the level.
public class CurrentLevel : $$anonymous$$onoBehaviour
{
public static int level;
public GameObject OnPlayNextbtn;
public GameObject quit;
Balloon b;
void Awake()
{
Chartboost.init ...
}
// Use this for initialization
void Start ()
{
level = PlayerPrefs.GetInt ("CurrentLv");
Chartboost.cacheInterstitial( "Startup" );
Chartboost.showInterstitial( "Startup" );
UILabel lab=GameObject.Find("HighScoreLabel").GetComponent<UILabel>();
lab.text = "[000000]Your Best for This Level is: [-][ff0000]" + PlayerPrefs.GetInt ("HighscoreLevel" + level, 0) + " [-]";
UILabel l=GameObject.Find("LevelNo").GetComponent<UILabel>();
if(level<10)
l.text="0"+level;
else
l.text=""+level;
Debug.Log("level "+level);
}
void Update()
{
}
public void OnPlayNext()
{
Balloon.miss = 0;
if (level == 0)
{
level=1;
}
else
{
level = PlayerPrefs.GetInt ("CurrentLv");
}
PlayerPrefs.SetInt ("CurrentLv",level);
PlayerPrefs.SetInt("Level"+level,1);
Application.LoadLevel("Choose");
}
public void OnQuit()
{
if (level == 0)
{
level = 1;
}
else
{
level = PlayerPrefs.GetInt ("CurrentLv");
}
PlayerPrefs.SetInt ("CurrentLv",level);
Debug.Log ("level beforre click" + level);
PlayerPrefs.SetInt("Level"+level,1);
Debug.Log ("In quit" );
Application.LoadLevel("Start");
}
How do I check to see if the FinishedLevel has completed loading?
Answer by _Yash_ · Jan 07, 2015 at 02:32 PM
You can make a boolean in the same script you handle your Button and check that boolean each time user clicks it.
Now enable/true that boolean after some time your FinishedLevel scene loads, for that you can invoke function with delay that enables that boolean from Start function.This should solve it.
Answer by Mmmpies · Jan 07, 2015 at 04:14 PM
Still not clear in your script, but when you get to your 10 correct click, before that button becomes enabled you need to do something like @Yash suggested.
keep a reference for your button script in the script that calculates when the 10th correct item has been clicked, let's call that CurrentLevelScript.
add a public bool in the script levelCompeted and start that as false. When the 10th object is clicked the first thing to do is
CurrentLevelScript.levelCompleted = true;
In OnQuit() check if levelCompleted = true. If it does set a puiblic float to = Time.time + 1f; Lets call that
private float OKToClick;
So if we get to 10 correct items levelCompleted is set to true. If the player catches the OnQuit() button that gets called but as the bool levelCompleted = true all it does is set OKToclick = Time.time + 1; and reset levelCompleted to false.
The put an else if (Time.time > OKToclick) do the quit button stuff, so:
pubilc bool levelCompleted = false;
private float OKToClick;
// rest of the script here
OnQuit()
{
if(levelCompleted)
{
OKToClick = Time.time + 1f;
levelCompleted = false;
}
else if(Time.time > OKToClick)
{
// do all your quit actions here
}
}
Sorry if it's not totally clear but I don't know the script details that handles the select right items.
And sorry to @Yash as this is pretty much what they said.
Your answer
Follow this Question
Related Questions
iOs/Android Development 2 Answers
How to change cameras by a single touch on my phone's screen? (Android) 1 Answer
how i can convert thjs code to touch (Input.anyKeyDown) 2 Answers
Android on phone 1 Answer
Read / Write to phone's contact list. 0 Answers