Unity游戏编程

时间:2012-04-13 13:37:46

标签: user-interface text unity3d

我的场景中有一个GUI Text对象,我想让它显示我剩下的游戏剩余时间。我似乎不能因为某些原因让这个工作。我有下面的代码,有人可以帮我吗?!

// the sound to play when the player is shot
public var shotSound:AudioClip;

// the number of lives
public var lives:int = 3;


/**
    Player has been shot
*/
function Shot () 
{
    // play the shot audio clip
    audio.PlayOneShot(shotSound);

    // reduce lives
    lives--;

    // reload the level if no lives left
    if (lives == 0)
    {
        // destroy the crosshair
        Destroy(GetComponent(CrossHair));

        // add the camera fade (black by default)
        iTween.CameraFadeAdd();

        // fade the transparency to 1 over 1 second and reload scene once complete
        iTween.CameraFadeTo(iTween.Hash("amount", 1, "time", 1, "oncomplete", "ReloadScene", "oncompletetarget", gameObject));
    }
}


/**
    Reload the scene
*/ 
function ReloadScene()
{
    // reload scene
    Application.LoadLevel("MainMenu");
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码。通过转到GameObject->创建其他 - > GUI文本来创建GUIText对象。现在将它拖到检查器面板中下面脚本的playerLives字段中。它应该工作。

// the sound to play when the player is shot
public var shotSound:AudioClip;

public var GUIText playerLives;

// the number of lives
public var lives:int = 3;

function OnGUI ()
{
    playerLives.Text = lives.ToString();
}
/**
    Player has been shot
*/
function Shot () 
{
    // play the shot audio clip
    audio.PlayOneShot(shotSound);

    // reduce lives
    lives--;

    // reload the level if no lives left
    if (lives == 0)
    {
        // destroy the crosshair
        Destroy(GetComponent(CrossHair));

        // add the camera fade (black by default)
        iTween.CameraFadeAdd();

        // fade the transparency to 1 over 1 second and reload scene once complete
        iTween.CameraFadeTo(iTween.Hash("amount", 1, "time", 1, "oncomplete", "ReloadScene", "oncompletetarget", gameObject));
    }
}


/**
    Reload the scene
*/ 
function ReloadScene()
{
    // reload scene
    Application.LoadLevel("MainMenu");
}