- Home /
Why is FindGameObjectsWithTag ignoring my Physics.OverlapSphere?
I've been racking my brains for ages over this one. My spaceship components, after spawning, do a quick check of the other components around them, in order to apply various effects to them. Simple enough, use a Physics.Overlapsphere, separate each component by using FindGameObjectsWithTag, throw them into arrays, and then Do Stuff to each item of the array as necessary. Code is here:
function FindLocalComponents () {
componentsInRange = Physics.OverlapSphere (transform.position, detectionRange) ;
for (var col : Collider in componentsInRange) {
tempsensorcomponentlist = GameObject.FindGameObjectsWithTag("sensor_component");
sensorcomponentlist = new Array (tempsensorcomponentlist) ;
tempweaponcomponentlist = GameObject.FindGameObjectsWithTag("weapon_component");
weaponcomponentlist = new Array (tempweaponcomponentlist);
tempshieldcomponentlist = GameObject.FindGameObjectsWithTag("shield_component");
shieldcomponentlist = new Array (tempshieldcomponentlist);
temprepaircomponentlist = GameObject.FindGameObjectsWithTag("repair_component");
repaircomponentlist = new Array (temprepaircomponentlist);
temphangarcomponentlist = GameObject.FindGameObjectsWithTag("hangar_component");
hangarcomponentlist = new Array (temphangarcomponentlist);
tempcargocomponentlist = GameObject.FindGameObjectsWithTag("cargo_component");
cargocomponentlist = new Array (tempcargocomponentlist);
}
}
detectionRange is an int, set to 20 Simple enough, right? Except it doesn't work correctly. If I view componentsInRange in the inspector, it works fine. It picks up only the colliders that I would expect to be within the radius. However, the temp_componenttype_list lists contain every component with the requisite tag, regardless of distance.
I just can't see where I'm going wrong. Have I missed something stunningly obvious? As far as I can make out, the parts of the script that create the lists and arrays should only operate on the colliders found in the Physics.OverlapSphere.
Answer by Kryptos · Jul 26, 2012 at 02:31 PM
GameObject.FindGameObjectsWithTag looks for all objects in the scene. It works separately from Physics.OverlapSphere (actually these two methods have nothing to do with each other).
I don't know where you get that Physics.OverlapSphere will somehow set a context so that later call to GameObject.FindGameObjectsWithTag will do something different. The fact that these two methods do not communicate with each other can easily be seen in your code, since the variable col
(in the for loop) is not used afterwards.
What you need to do is to check for the tag of the found colliders inside the for loop:
for (var col : Collider in componentsInRange) {
if (col.gameObject.tag == "sensor_component")
{
// assuming sensorcomponentlist is a list (System.Collections.List)
sensorcomponentlist.Add(col.gameObject);
}
// and so on...
}
I don't know why I thought they'd work together. It was a script I put together a while ago, and only tested on a small static sample of components (which is why I didn't pick it up until I tried a large-scale test yesterday) I used your example and rewrote it all, and it's working like a winner now, as well as being more efficient all-round. Thanks for your help, it's greatly appreciated.
As a general rule, don't use the GameObject.Find methods at runtime as they are much too expensive. You can use them in Awake or Start methods to get references to objects and use those references afterwards.
The way it works is that they're only called at the start. First, I spawn all the components, then I run FindLocalComponents() but it only gets called once for each component. This is before control is given to the player, so it's worth the temporary hit.
Great answer! I was not sure if you are able to search for a tag of a collider using Overlap Sphere!
Your answer
Follow this Question
Related Questions
FindWithTag in certain range 2 Answers
Find child of a Game Object using tag 4 Answers
Destroy gameobject by tag (.js) 3 Answers
How to disable scripts inside objects found with tag 1 Answer
AI Turret Targeting System 2 Answers