- Home /
Complete Level Script
Hi,
I'm trying to see where I am going wrong with my script once a player completes the level. I need to have the screen to say "Try Again!" and the player lose a life if they score below 1500 points. I also need the screen to say "Level Cleared!" if the player score above 1500 points. Here is my script:
public class PlayerResults : MonoBehaviour { public bool Result1 = false; public bool Result2 = false;
void OnLevelWasLoaded(int level) {
if (level == 8)
if (Score.score < 1500) {
Result1 = true;
Lives.curLives--;
}
if (Score.score > 1500) {
Result2 = true;
}
}
void ResultText(bool Result1, bool Result2) {
if (Result1 = true)
{
guiText.text = "TRY AGAIN!";
guiText.material.color = Color.black;
}
if (Result2 = true)
{
guiText.text = "LEVEL CLEARED!";
guiText.material.color = Color.black;
}
}
}
Thanks.
Answer by Azmodii · Aug 28, 2013 at 08:39 AM
You need to add this line;
ResultText ( Result1, Result2);
Add it like so;
void OnLevelWasLoaded(int level) {
if (level == 8)
if (Score.score < 1500) {
Result1 = true;
Lives.curLives--;
}
if (Score.score > 1500) {
Result2 = true;
}
ResultText ( Result1, Result2);
}
As far as I can see, you werent actually "calling" the function.
It would also be a lot clearer to use only one Bool here.
IsScoreEnough.
If its false, it will say try again, if its true, it will say level cleared. You can get rid of two if statements then.
So I made adjustments like you suggested, but it seems to be calling the function before I need it. The level cleared text shows before the OnLevelWasLoaded level, but I checked and I selected the correct level to show the text.
Here is my code:
public bool IsScoreEnough = false;
void OnLevelWasLoaded(int level) {
if (level == 8)
if (Score.score < 1500) {
IsScoreEnough = false;
Lives.curLives--;
} if (Score.score > 1500) {
IsScoreEnough = true;
}
ResultText (IsScoreEnough);
}
void ResultText(bool IsScoreEnough) {
if (IsScoreEnough = false)
{
guiText.text = "TRY AGAIN!";
guiText.material.color = Color.black;
}
else if (IsScoreEnough = true)
{
guiText.text = "LEVEL CLEARED!";
guiText.material.color = Color.black;
}
} }
I made a few changes to my level complete code, but it has 2 issues:
Firstly it does not show the "S$$anonymous$$ILL CLEARED!" text anymore and the second issue is that my TimerSpentText does not appear either. I'm not sure what is wrong.
public class PlayerResults1 : $$anonymous$$onoBehaviour {
public bool IsScoreEnough = false;
void OnLevelWasLoaded(int level) {
if (level == 8)
{
if (Score.score < 1500)
{
IsScoreEnough = false;
Lives.curLives--;
}
if (Score.score > 1500)
IsScoreEnough = true;
}
ResultText(IsScoreEnough);
}
void ResultText(bool IsScoreEnough)
{
if (IsScoreEnough == false)
{
guiText.text = "TRY AGAIN!";
guiText.material.color = Color.black;
}
else if (IsScoreEnough == true)
{
if (Score.score > 1500 && Score.score < 3000)
Application.LoadLevel("Bronze1");
else if (Score.score > 3000 && Score.score < 4500)
Application.LoadLevel("Silver1");
else if (Score.score > 4500)
Application.LoadLevel("Gold1");
{
guiText.text = "S$$anonymous$$ILL CLEARED!";
guiText.material.color = Color.black;
GameObject.Find ("TimeSpentText").guiText.text = " " + Skill1Timer.finishTime.ToString();
guiText.material.color = Color.black;
}
}
}
}
Your answer
Follow this Question
Related Questions
Help with different medals for completing a level 1 Answer
Multiple Cars not working 1 Answer
Creating a level from a textfile 0 Answers
Distribute terrain in zones 3 Answers
Disabling a script in c# not working for some reason 1 Answer