Miguel Gaurano Punzal Miguel Gaurano Punzal - 8 months ago 43
C# Question

Display GUI with incrementing variable each collision

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) {
capscore =capscore+1;

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


Answer Source

Like this .. very easy

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

    void OnTriggerEnter(Collider other) {
        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