Unity游戏编程
问题描述:
我在场景中有一个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");
}
答
请尝试以下代码。通过转到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-13 13:42:19
基本上,场景中的GUI文本不会更新。它保持为它设置的通用文本。我在更新函数中使用了(guiText.text =“Lives Remaining:”+ lives;)代码,并将它作为GUI文本的一个组件,但它似乎不起作用? – user1270217 2012-04-13 13:51:59
是否有机会发布您尝试过的一些(任何)代码?我甚至没有看到这个代码中存在'update()',也没有提及'guiText'。也许我错过了一些东西,但是如果有人愿意帮助这个人,我想更多的信息是需要的。 – Lance 2012-04-13 19:28:36