Game Development Reference
In-Depth Information
We can also send a message that defines one of the variables (like a Boolean)
in much the same way:
GameObject.GetComponent(“name of script”).BooleanVariable
= true;
Eventually we're going to want to do both in the inventory script; we're going
to want Unity to understand that when we click a button, it needs to activate
a particular Boolean in AC_ToolFunctionality.
We'll be talking to this same script a lot, so we can shorten things a bit by
declaring some additional variables and then referencing those variables
throughout the script.
Step 18: Open InventoryButtonScript. Create a new variable to contain
the object that holds the script we'll be referencing. Then have the script
find Main Camera on Awake.
var buttonReg : Texture2D;
var buttonOver : Texture2D;
private var mainCamera : GameObject;
function Awake(){
mainCamera = GameObject.Find(“Main Camera”);
}
Why?
Remember that AC_ToolFunctionalityScript is attached to the Main Camera
that is a child of our prefab FPC_AegisChung. So in order to reference that
script, we need to make sure this script knows where that object is.
Now, we're taking a calculated risk here. In this case, we're making the var
mainCamera a private variable, and having the function Awake go and find it.
This is in contrast to the method we've been using of manually defining public
variables within Unity. In this case, we're making the reasonably safe assumption
that there will only be one object named “Main Camera” in the scene. And
since this script is attached to three objects, having Unity go and find it on start
is a bit easier on us since we needn't populate the variables manually.
Step 19: Create another private variable that will hold the reference to the
actual script; then have this script also found on awake:
var buttonReg : Texture2D;
var buttonOver : Texture2D;
private var mainCamera : GameObject;
private var toolFunctionality;
function Awake(){
mainCamera = GameObject.Find(“Main Camera”);
toolFunctionality = mainCamera.GetComponent(“AC_Tool
FunctionalityScript”);
}
 
Search WWH ::




Custom Search