2

I have a GUI on my scene saying "Capsules Collected: 0/10" and a capsule object which has Collider that whenever the player enters the capsule , the capsule will be destroyed and the Capsules Collected will be increased by 1.

The Destroy Works well, the GUI isnt displaying. What is wrong with my code?

Here's my Code, I assigned this C# script on the Player itself:

using UnityEngine;
using System.Collections;


public class CapsuleGET : MonoBehaviour {
    int capscore=0;

    void Start(){
    }

    void OnTriggerEnter(Collider other) {
        Destroy(other.gameObject);
        capscore =capscore+1;
    }

    void Update(){
        GUILayout.Label("Capsules Collected: "+capscore+"/10");
    }

}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Hey Miguel - you can't really use the "ancient GUI system" now, it is totally deprecated. The new system is incredibly easier ......... here http://stackoverflow.com/a/36268018/294884 – Fattie Nov 25 '16 at 12:40
  • As a side note I would not recommend setting the value of the label on **every update** instead do it inside the `OnTriggerEnter` method – Alfie Goodacre Nov 25 '16 at 12:49

1 Answers1

4

Like this .. very easy

using Unity.UI;
public class CapsuleGET
   public Text displayScore;  // DRAG to connect in Editor

    void OnTriggerEnter(Collider other) {
        Destroy(other.gameObject);
        capscore =capscore+1;

        displayScore.text = capscore.ToString();

1 - click to add canvas (don't forget to select 'scale with screen size')

enter image description here

2 - click to add Text, position as you want.

3 - in your script, "public Text score"

4 - drag from the "Text" to that variable

explanation with diagrams

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719