Display Message Depending On Score?

So I want to display a message at the Game Over screen. Example, If the player scores less than five points then show this message. If they score more than 5 but less than 10 show this message, else if they score more than 10 but less than 20 show this message and so on… This is my code. I get no errors so its working fine… But it only shows the lowest score. Ive tried multiple ways of recoding. None with an error, yet still only shows the lowest score. I think it may be becuase my correct score script and wrong score script call on the score value . and as you see in the show message script im calling on the score wich is pulling from both right and wrong answer scripts which is a problem. I dont really want to have to re write the scoreing just to show a message… Any help is appreciated.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Score_Message : MonoBehaviour {

    public static int score;
    public Font MyFont;
  
    Text text;
  
  
    void Start()
    {
        text = GetComponent<Text> ();
      

    }
  
    void OnGUI()
    {
        GUI.skin.font = MyFont;
        if (score >= 30)

            GUI.Label (new Rect (250, 400, 500, 150), "<color=white><size=30>We Bow In Your Presence!</size></color>");
       
            else if (score <= 29  && score > 20)
                GUI.Label (new Rect (250, 400, 500, 150), "<color=white><size=30>You Can See Into The Future. What Can You See?</size></color>");
            else if (score <= 19 && score > 10)
                GUI.Label (new Rect (250, 400, 500, 150), "<color=white><size=30>You Have The Eyes Of A Scout. Keep Your Eyes Open!</size></color>");
            else if (score <= 9 && score > 5)
                GUI.Label (new Rect (250, 400, 500, 150), "<color=white><size=30>You Have The Eyes Of A Mole Rat. Need Some Glasses?</size></color>");
            else if (score <= 4)
                GUI.Label (new Rect (250, 400, 500, 150), "<color=white><size=30>You're Not Even Trying.. Are You?</size></color>");




    }

  
  
  
  
}

The Right Score Script

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Score : MonoBehaviour
{
    public static int score;


    Text text;


    void Start()
    {
        text = GetComponent<Text> ();

        score = 0;

        score = PlayerPrefs.GetInt ("CurrentScore");


    }

    void Update()
    {
        if (score < 0)
            score = 0;

        text.text = "" + score;
    }

    public static void AddPoints (int pointsToAdd)
    {
        score += pointsToAdd;
        PlayerPrefs.SetInt ("CurrentScore", score);

    }



    public static void Reset()
    {

        PlayerPrefs.SetInt ("CurrentScore", score);
        score = 0;
    }


  
  




}

And The Wrong Score Script.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Score_Wrong : MonoBehaviour
{
    public static int score;
  
  
    Text text;
  
  
    void Start()
    {
        text = GetComponent<Text> ();
      
        score = 0;
      
        score = PlayerPrefs.GetInt ("CurrenttScore");
    }
  
    void Update()
    {
        if (score < 0)
            score = 0;
   
  



        text.text = "" + score;



  
      
    }

    //public void onClick(){
    //    if (score == 4)
    //        Application.LoadLevel ("Game_Over");
    //    text.text = "" + score;
    //}
  
    public static void AddPoints (int pointsToAdd)
    {
        score += pointsToAdd;
        PlayerPrefs.SetInt ("CurrenttScore", score);
    }
  
    public static void Reset()
    {

        PlayerPrefs.SetInt ("CurrenttScore", score);
        score = 0;
    }
  
  
  
  
  
  
  
  
}

The Score Sytems that calls on the scores to be set in text. Both are the same with different add point names to not have reoccurring names in both scripts and to divide the two for right and wrong scoring.

using UnityEngine;
using System.Collections;

public class Score_System : MonoBehaviour {

    public int pointsToAdd;



    // Use this for initialization

    public void onClick () {




        Score.AddPoints(pointsToAdd);



    }
  
    // Update is called once per frame
    void Update () {
  

    }
}

This is a screen shot

Is should be showing the message for the correct score 10… but instead its showing for <5…

Few things:

  • All of these static scores aren’t shared and you never set Score_Message’s score value anywhere so it will default to zero.

  • Your if / else statements for the text is missing cases for exactly 5, exactly 10 and exactly 20 so the only reason you see anything is because of the first issue.

  • You definitely don’t need to make three forum posts within 15 minutes to bump your thread.

I wasnt trying to bump my thread lol… I forgot to add those statements in with first post. My bad

And i set Public score int in my main score script. so the others pull from that. thats how i reference my score into the gameover screen. It pulls from the other scripts. So i figured id do the same with the message script and pull the total score value and set a message depending on those values. and i dont want it to be exaclty 5, 10, and 20. thats why i put <=5 && > 10 to be set in between those values.

Public or not, you still never set it. Where’s the code that sets Score_Message.score = any value? You get the score from PlayerPrefs with the other scripts but not in Score_Message, so it has a value of zero. You can check that by putting Debug.Log(score); above your big if/else block.

Seems odd to me that you don’t want any message at all to show up at 5, 10 or 20 when you’re complaining about the wrong message showing up when they score 10 points… so maybe I’m missing something.

Solved!! You were absolutly right. I didnt set Playerpref inside message script. The game over screen calls on the player preference. So I set that inside start and it works …

void Start()
    {
        text = GetComponent<Text> ();
        score = PlayerPrefs.GetInt ("CurrenttScore");
        score = PlayerPrefs.GetInt ("CurrentScore");


    }
   
    void Update()
    {
        if (score == 0)
            text.text = "You're Not Even Trying Are You?";
        else if (score >= 5)
            text.text = "You Have The Vision Of A Mole Rate.. Need Some Glasses?";
        else if (score >= 10)
            text.text = " Your Have The Vison Of A Scout.. Keep Your Eyes Peeled";
        else if (score >= 20)
            text.text = "You Can See Into The Future.. What Do You See?";
        else if (score >= 30)
            text.text = "We Bow In Your Presence";






    }

Thanks bud. I was missing it.

Your new if/else block isn’t going to work out either. A score of 30 is >= 5, so you’ll always see the second message. You need to put it the other way around from highest value to lowest value.

Wouldnt it pull from the lowest first? So if the score is 0 itll show the message for that else if the score is >=0 && score < 5 itll show that message… This is my new set up any how.

void Update()
    {
        if (score == 0)
            text.text = "You're Not Even Trying Are You?";
        else if (score >= 0 && score < 5)
            text.text = "You Can Do Better..";
        else if (score >= 5 && score < 10)
            text.text = "You Have The Vision Of A Mole Rate.. Need Some Glasses?";
        else if (score >= 10 && score < 20)
            text.text = " Your Have The Vison Of A Scout.. Keep Your Eyes Peeled";
        else if (score >= 20 && score < 30)
            text.text = "You Can See Into The Future.. What Do You See?";
        else if (score >= 30)
            text.text = "We Bow In Your Presence";






    }

So it should start with 0 and say if its not = to 0 than is it > than or = to 0 but < than 5? Then show this message… else if its > or = to 5 but < than 10 then show this message… Get where im coming from?

Yes, that’s the problem, that it would pull from the lowest first. If you score a billion points, then it would go:

if (score == 0) // one billion isn’t equal to zero, let’s try the next one
else if (score >= 5) // one billion is greater than or equal to 5, lets use this message

Your new code is fine as it makes sure it doesn’t exceed the next else statement’s minimum.

Its working just fine for me… Included are some screen shots. One is with a score of 1 correct wich says you can do better. and the other is with a score of 8 which says you have the vision of a mole rat… seems its working


i see what your saying. let me go above 10 points and see if that message posts

I was talking about your old if/else code mate. You missed this line in my post:

Yeah it works fine. Its set up so itll go down the line until its over that current score. so if say they did get 10000 points itll only show the message for >=30. heres the screen.

yeah, thanks bud