Game Development Reference
In-Depth Information
Debug.Log (“Selected object is ” + hit
.collider.gameObject.name);
}
}
function AddHighlight(hiOb:GameObject){
hiOb.renderer.material.color.r = hiOb.renderer
.material.color.r*10;
hiOb.renderer.material.color.g = hiOb.renderer
.material.color.g*10;
hiOb.renderer.material.color.b = hiOb.renderer
.material.color.b*10;
yield WaitForSeconds (0.1);
hiOb.renderer.material.color.r = hiOb.renderer
.material.color.r/10;
hiOb.renderer.material.color.g = hiOb.renderer
.material.color.g/10;
hiOb.renderer.material.color.b = hiOb.renderer
.material.color.b/10;
}
Why?
Lots to discuss here. First, what we're creating here is a new function that
we can call on at other places within this script, or even call from other
scripts. Of course, at this point we haven't called out to use this function
quite yet, but we'll get to that in a bit. For now, let's look at what this
script will do when it's called.
The first line simply creates a name for the new function ( AddHighlight ).
But additionally, it declares a new variable that will be used just within this
function ( hiOb:GameObject ) but can be called to from other functions ( hiOb
is just short for highlightObject and is a term I made up to use here—you
could name it differently). Specifically, this means that when we call for this
script we can call for it and include what the GameObject is that this function
will use for hiOb. We're essentially allowing a variable of one function to be
populated by another function elsewhere. Really powerful stuff.
Now below that, in the meat of the meal are lines that just say, “take the
object to highlight, look in its renderer component, find the material,
and then take whatever the red value is for the color of the material and
multiply it by 10.” Then do this for the green and blue values and the net
results will be that the object highlights in bright white.
After that is a little trick that essentially says, “wait for 1/10 th of a second,
and then change it all back.” The reason we're doing this is that we want
to make sure that when the ray is no longer casting on the object to
highlight, the object turns back to the way it was.
The function AddHighlight being separate means we can reuse this chunk of
code multiple times without retyping it. We can start to develop mechanisms
 
Search WWH ::




Custom Search