- Home /
Question by
oinkoinkflapflap · Mar 15, 2011 at 08:19 PM ·
javascriptdistancetag
tag distance script
hi, i have a distance script for a transform var... but i have the problem of only 1 var, and i need instantiated prefabs ect... how can i make a distance script that activates when any object with a certain tag is near... i need it to enable a script (GetComponent("gameOver").enabled = true;) thanks!!!! :D
Comment
Answer by Bunny83 · Mar 15, 2011 at 10:26 PM
You can use Physics.OverlapSphere to get all colliders that are in "range". Then you can check for a special tag name or what ever.
var allColliders : Collider[] = Physics.OverlapSphere(transform.position, 5.0);
for (var currentCollider : Collider in allColliders)
{
if (currentCollider.tag == "MyTagName")
{
GetComponent.<gameOver>().enabled = true;
}
}
Answer by zmar0519 · Mar 15, 2011 at 08:28 PM
I suggest that you attatch a sphere collider to the object, or make it a child. Set the collider to trigger, and in OnTriggerEnter
, check if the object has the tag. if so, then execute your get component.