2012-04-13 214 views
0

我在場景中有一個GUI文本對象,我希望它能顯示我留下的剩餘生命。我似乎無法得到這個工作出於某種原因。我有下面的代碼,有人可以幫我嗎?!Unity遊戲編程

// 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

它不起作用?細節會幫助那些想要幫助的人。 – 2012-04-13 13:42:19

+0

基本上,場景中的GUI文本不會更新。它保持爲它設置的通用文本。我在更新函數中使用了(guiText.text =「Lives Remaining:」+ lives;)代碼,並將它作爲GUI文本的一個組件,但它似乎不起作用? – user1270217 2012-04-13 13:51:59

+1

是否有機會發布您嘗試過的一些(任何)代碼?我甚至沒有看到這個代碼中存在'update()',也沒有提及'guiText'。也許我錯過了一些東西,但是如果有人願意幫助這個人,我想更多的信息是需要的。 – Lance 2012-04-13 19:28:36

回答

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