0

我的场景中有一个 GUI 文本对象,我希望它显示我为戏剧留下的剩余生命。由于某种原因,我似乎无法让它工作。我有下面的代码,有人可以帮我吗?

// 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");
}
4

1 回答 1

0

试试下面的代码。通过转到 GameObject->Create Other->GUI Text 创建 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");
}
于 2012-04-14T09:49:33.237 回答