2012-04-13 3 views
0

Ich habe ein GUI-Text-Objekt in meiner Szene und ich möchte, dass es die verbleibenden Leben zeigt, die ich für das Spiel übrig habe. Ich kann es aus irgendeinem Grund nicht schaffen. Ich habe den Code unten, könnte mir bitte jemand helfen ?!Unity Game Programmierung

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

Wie funktioniert es nicht? Details würden denen helfen, die helfen wollen. –

+0

Im Grunde wird der GUI-Text in der Szene nicht aktualisiert. Es bleibt als generischer Text bestehen. Ich verwendete (guiText.text = "Lives Remaining:" + lebt;) Code in der Update-Funktion und machte es zu einer Komponente des GUI-Textes, aber es scheint nicht zu funktionieren? – user1270217

+1

Gibt es eine Chance, dass Sie (irgendeinen) Code, den Sie ausprobiert haben, veröffentlichen können? Ich sehe weder eine 'update()' in diesem Code, noch irgendeinen Hinweis auf 'guiText'. Vielleicht verpasse ich etwas, aber ich denke, dass mehr Informationen benötigt werden, wenn jemand mit dieser Hand helfen soll. – Lance

Antwort

0

Versuchen Sie den folgenden Code. Erstellen Sie ein GUIText-Objekt, indem Sie zu GameObject-> Create Other-> GUI Text gehen. Ziehen Sie dies nun in das playerLives-Feld unterhalb des Skripts im Inspektorfenster. Es sollte funktionieren.

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