Game Development Reference
In-Depth Information
the function Start would remain dormant but would fire up as soon as the
object was activated. However, anything that is in a function Awake will still
fire whether the object is active or not.
The reason all this matters to us is we can have the script go and find our
two DataPanels and populate the variables of the OpenSceneButtonsScript
for us (no need to manually populate). For most cases where we're defining
variables, function Awake should be used because the definitions should
happen right at the beginning.
Step 40: Reopen OpenSceneButtonsScript . Replace the variable
declarations with the following:
Warnings and Pitfalls
Some of the lines of
code are longer than
the format of a topic will
allow. Remember that
there is usually a new
line at the end of each
statement, and that each
statement ends with a ";".
So if a line shown here
doesn't end with a ";" or
a "{" (for blocks of code),
keep it as a solid line of
code.
private var dataPanelBiography : GameObject;
private var dataPanelMission : GameObject;
function Awake (){
dataPanelBiography =
GameObject.Find(“DataPanel_Biography”);
dataPanelMission =
GameObject.Find(“DataPanel_Mission”);
}
function OnMouseEnter () {
guiTexture.color = Color (1,1,1);
}
function OnMouseExit (){
guiTexture.color = Color (.2,.2,.2);
}
Warnings and Pitfalls
GameObject.Find is
a very handy tool to
have. However, it can
quickly be abused. For
instance, if GameObject.
Find is contained within
a function Update, this
means that Unity is
being asked to go find
an object on every single
frame. Very many of
these and suddenly Unity
is more busy looking for
objects than it is running
the game. Generally,
use GameObject.Find
when it can fire once
(like in function Awake
situations or within
another function that is
called at a very specific
time).
function OnMouseDown(){
if (name == “Button_Bio”){
dataPanelBiography.guiTexture.enabled = true;
dataPanelMission.guiTexture.enabled = false;
}
if (name == “Button_Mission”){
dataPanelMission.guiTexture.enabled = true;
dataPanelBiography.guiTexture.enabled = false;
}
if (name == “Button_Begin”){
Application.LoadLevel (“Scene-EntryWay”);
}
}
Why?
There are two things happening here. First, private was added before
each of the variable declarations. A private variable is a variable that can
be defined, but it isn't defined in the Unity Editor via dragging items as
we did before. Although these could remain public, since they are going
to be defined in the script, it keeps another developer from trying (or
feeling like they need) to populate variables.
Search WWH ::




Custom Search