- Home /
Prefab C# : Level Restart Alert problem : Android
Hello , I'm a newbie in programming C# I am developing a game , I created a prefab of a picture alert which will appear when the user has failed to complete the stage it says "Tap to restart level" the function i want for it is when the picture is tapped the game will restart its level now the problem is whenever the user taps anywhere on the screen the game will restart even if the game is not failed.
Here's the code I created, theres something wrong with it . Thanks for taking your time seeing this .
using UnityEngine;
using System.Collections;
public class Restart : MonoBehaviour
{
void Update()
{
if (Input.GetButton("Fire1"))
{
Reload();
}
}
void Reload()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Answer by Zorkman · Mar 18, 2016 at 01:17 PM
Hello!
The problem with your script is that it is constantly listening for clicks on the button assigned to "Fire1", which seems to be your mouse button, and when a click is triggered then the function Reload()
is executed.
I recommend that you take a look into Unity's UI system and try to build it with that, there are tons of resources and tutorials on this subject. Check out https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button for information about how to use the UI Button. Then you can assign your reload function so it is called when the button is pressed.
Answer by Ali-hatem · Mar 18, 2016 at 02:16 PM
you have to test if the user has failed to complete the stage & then test button "'Fire1" is pressed then load level :
void Update()
{
if(playerfaild) // what ever how you decide the player faild
{
if (Input.GetButton("Fire1"))
Reload();
}
}
Your answer

Follow this Question
Related Questions
How to fix bold black bars on right side of the Game (on any device) 1 Answer
What is common screen resolution for android game to be selected 0 Answers
Why can't GameObject.Find find more than one object at the start function? 1 Answer
The Chrome book renders only 1 image. 0 Answers
General purpose questions for Unity PRO 2 Answers