简体   繁体   中英

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) {
        Destroy(other.gameObject);
        capscore =capscore+1;
    }

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

}

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')

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM