Game Development Reference
In-Depth Information
//If this is shortest, then update
if(Distance < ShortestDistance)
{
//Is shortest, now update
Nearest = Obj;
ShortestDistance = Distance;
}
}
//Return nearest
return Nearest;
}
Finding any object of a specified type
Sometimes, you just want a list of all the components of a specified type in the scene,
regardless of which game objects they're actually attached to; these components
include all enemies, all collectible objects, all transform components, all colliders, and
so on. Achieving this from script is simple but expensive, as shown in the following
code sample 3-6. Specifically, by calling the Object.FindObjectsOfType function,
you can retrieve a complete list of all instances of a specified object in the scene,
unless an object is deactivated. Due to the expense of this method, avoid calling it
during frame-based events such as Update . Use the Start and Awake events, as well
as infrequent functions instead:
void Start()
{
//Get a list of all colliders in the scene
Collider[] Cols = Object.FindObjectsOfType<Collider>();
}
 
Search WWH ::




Custom Search