Display the score in unity

人走茶凉 提交于 2020-01-05 13:09:00

问题


I am completely new to unity. I am making a simple 2d platformer game using unity. Can somebody help me to display the score on the game. I am storing the value on a int variable. Below is the c# code i used for the distance covered.

using UnityEngine;
using System.Collections;
public class distanc : MonoBehaviour {
    private int dist;

    // Use this for initialization
    void Awake () 
    {
        dist = 0;
    }

    // Update is called once per frame
    void Update () 
    {
        dist = dist+=1 * Time.deltaTime;
        print("Dist:" + dist);
    }
}

what i want is to display the dist value on the screen. I have placed a GUI Text on the screen.


回答1:


Making it simple, you can do - where you are printing the dist - something like this:

GameObject.Find("GUIText").guiText.text = dist;

, supposing that your GUIText object is called GUIText. Basically, with that, you will find an object of that name, access its text component, and modify it with dist value.



来源:https://stackoverflow.com/questions/29078714/display-the-score-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!