Game Development Reference
In-Depth Information
function Awake (){
healthLevel = GameObject.Find(“HealthLevel”);
}
function Start(){
health = 100;
healthLevel.guiText.text = health.ToString();
}
Why?
The first command of the Start function simply defines the value of
health. Later we will want to pass this value between levels so that if Aegis
gets into trouble in the EntryWay, that his reduced health is the same
when he's in the Hallway. But for now, let's just have it set to 100 when
the game starts.
The second command defines what should be in the text of the guiText
component of the GameObject healthLevel. Notice that this makes use
of the fancy ToString function. This is a built in function in Unity that
converts (in this case) an integer (which a guiText can't display) to a string
(which a guiText can display). So it just takes the value of health, converts
it to string (a collection of text), and displays that. Seems a little laborious
but it's typical of how many scripting languages work.
Now we haven't created the guiText GameObjects yet. We'll get to that
in a bit.
Step 4: Save and return to Unity. Fix syntax.
Creating Health Display
Step 5: Open Scene-EntryWay.
Step 6: Create a new GUITexture (GameObject>Create Other>GUI
Texture). Label it HealthLabel and populate its Texture with the
HealthBG texture from the 2D Assets folder. Position it with the
settings shown in Figure 17.5 . Make sure Transform X =.5 , Y = 1 . Then
be sure the Pixel Inset values are set to X = -32 , Y = -64 , Width = 64 ,
Height= 64 .
Why?
The image is 64×64 (thus the width and height settings). By setting the
Transform values at 0.5, 1 we're putting the bottom-left corner of the
image at the center of the top of the screen. Offsetting the image by
X = -32 slides the middle of the image to the middle of the screen, and
then offsetting the image by Y = -64, slides it down so the top of the
image is at the top of the screen.
Search WWH ::




Custom Search