Wyh doesn´t the hiScore work?

Hi i have this score-and hiScore scipt! The (downloaded part) Score works fine, but the (self written part) hiScore doesnt… why?

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour
{
     public bool isCounting = true;
     public float timer = 0f;
     public int points = 0;
     public int hiScore;
     private GUIStyle guiStyle = new GUIStyle();
    void Start()
    {
    }

    public void OnObjectClicked()
    {
        isCounting = !isCounting;
    }

    void Update()
    {
        if (isCounting)
        {
            timer += Time.deltaTime;
            if (timer > 1f)
            {
                points += 1;
                timer -= 1f;
            }
        }
    }
    void FixedUpdate()
    {
        if (points > hiScore)
        {
            hiScore = points;
            PlayerPrefs.SetInt("HighScore", hiScore);
        }
    }
    void OnGUI()
    {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(105, 22, 100, 100), points.ToString(), guiStyle);

    }
    void OnGUI2 ()
    {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(205, 22, 100, 100), hiScore.ToString(), guiStyle);
    }
}

Did you put a print in your fixedupdate? is it running? Is the if statement running?

Yes everything works but it is not displayed on the screen…

you can’t do this. Making up function names isn’t going to magically make the engine hook into them.

If you want two labels to render on the screen using the old OnGui approach it all goes into the OnGUI function.

Good catch. Overlooked that since I haven’t used OnGui in a long time. @EmotionIce Yep, this is correct.

you need to an if() statement, and a way bool named useGuiA. but your current code does have a way to switch on and off the bool. we don’t know how you intend to use the two gui text boxes.

void OnGUI()
    {
        if(useGuiA)
        {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(105, 22, 100, 100), points.ToString(), guiStyle);
        }else
        {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(205, 22, 100, 100), hiScore.ToString(), guiStyle);
        }
    }

if you want both gui boxes to display at the same time

void OnGUI()
    {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(105, 22, 100, 100), points.ToString(), guiStyle);
        GUI.Label(new Rect(205, 22, 100, 100), hiScore.ToString(), guiStyle);
       
    }
1 Like

@johne5 thank you so much!!! It worked perfectly :slight_smile: