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 に答える