Game Development Reference
In-Depth Information
It would also make sense to hide the HUD until the garden is entered. For the GUI Text, you will
disable the GUILayer component on the GUI Camera. The battery sprites are gameObjects, so the
simplest solution to control them will be to put them into a group and activate and deactivate the
group. The problem with this is that deactivated objects cannot be “found.” In most cases, you
identify and store the object in an Awake function before it has been deactivated so the script can
turn it back on later in the game. If it doesn't exist in the current level or scene, you can turn off
the renderer component. In this case, there are only two objects, so you could easily turn them off
individually. If you had several objects, you would want a way to iterate through them and do the
required processing in a more generic and flexible manner. To keep the code reusable, you can make
a simple script that can be called from several different places.
6.
Create a new Empty gameObject, and name it Garden HUD .
7.
Drag, Battery_0, and Battery_1 into it in the Hierarchy view.
8.
Create a new C# Script in the Game Scripts folder, and name it
ChildVisibility .
9.
Add the array variable that will hold the children of the object the script is on:
public Component[] spriteRenderers;
10.
Also add a variable to hold the Camera GUI's GUILayer:
public GUILayer hudText; // the Camera GUI's GUILayer component
11.
Create the function that does the work:
public void SpriteToggle(bool newState) {
spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer sprite in spriteRenderers) {
sprite.enabled = newState;
}
hudText.enabled = newState;
}
In this function, you are creating an array of all of the object's children with a Sprite Renderer
component, and then iterating through it and turning it on or off depending on the state passed in to it.
In the Start function, find the GUILayer and turn off the sprites and text:
12.
hudText = GameObject.Find("Camera GUI").GetComponent<GUILayer>();
SpriteToggle (false); // turn off at start
13.
Save the script, and add it to the Garden HUD object.
Next you will block the automatic start-up zombie bunny drops.
 
Search WWH ::




Custom Search