0

I'm working a unity 3d game and I've ran into a problem:

I have two boxes called breakableBox and breakableBox_2. When the player collides with them, they add to the player's score variable playerScore and the box hides itself. Here's the code that both of the boxes use:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public static int playerScore;

    public Renderer rend;

    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = true;
    }

    void OnTriggerEnter(Collider other)
    {
        rend.enabled = false;
        playerScore++;
    }



}

And then in order to show the score, I have this script attached to the player camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Points : MonoBehaviour {
    int score = ExampleClass.playerScore;
    void OnGUI()
    {
        GUIStyle style = new GUIStyle(GUI.skin.button);
        style.fontSize = 24;
        GUI.Label(new Rect(1, 1, 150, 30), score.ToString() + " points", style);
    }
}

However, the score stays at zero, even when in the console I can see that it added the points to the variable. If anyone knows how to help me fix this, that would be great.

2
  • 1
    It would probably be better practice to maybe set up your score change as an event. So you can just have your different scripts subscribe to that event that way the score only gets updated when it is changed. Commented May 31, 2018 at 15:17
  • 2
    Stop using OnGui and use the new UI instead. Commented May 31, 2018 at 16:50

1 Answer 1

2

You are not updating the score int in your Points class, try this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Points : MonoBehaviour {

    void OnGUI()
    {
        int score = ExampleClass.playerScore;
        GUIStyle style = new GUIStyle(GUI.skin.button);
        style.fontSize = 24;
        GUI.Label(new Rect(1, 1, 150, 30), score.ToString() + " points", style);
    }
}

Edit: as @MXD mentioned in the comment, it's better to not update values in OnGUI and do it in Update() Instead [or FixedUpdate() since your score system is physics reliant].

Sign up to request clarification or add additional context in comments.

4 Comments

Performance wise it may be better to call it in an update instead of an OnGUI. As the OnGUI is called significantly more then an update.
@MXD yeah, also he'd better not store the score the way he does, but I'm just answering his specific question with the least amount of code :D I upvoted your comment
It never hurts to give good advice along side good answers ;)
To be more precise: He/She should be using the ui system and not the immediate mode gui.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.