- Home /
Get component in FindGameObjectsWithTag...?
I'm trying to get a specific script that's on all game objects marked with a certain tag but I'm having a bit of difficulty understanding how FindGameObjectsWithTag works. Here is what I tried:
public HitReaction hitReaction;
void Awake(){
hitReaction = GameObject.FindGameObjectsWithTag("HitReactor").GetComponents<HitReaction>();
}
(I need to use hitReaction later on in the script with a Raycast hit)
But I get the error: Type UnityEngine.GameObject[]' does not contain a definition for
GetComponents' and no extension method GetComponents' of type
UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
Clearly that's not the way to do this. I looked through the manual for FindGameObjectsWithTag but I couldn't find any examples using it with GetComponent. Any advice?
Answer by kacyesp · Sep 01, 2014 at 10:17 PM
FindGameObjectsWithTag returns an array of GameObjects. You can only call GetComponents on a single GameObject, not an array.
Since it looks likes you're only looking to store a single HitReaction, I'm assuming you only have one HitReactor? If that's the case, then is this what you were trying to do?
public HitReaction hitReaction;
void Awake(){
hitReaction = GameObject.FindWithTag("HitReactor").GetComponent<HitReaction>();
}
If you have multiple HitReactors, and you want to store the HitReaction for each one, then I think this is what you want:
public HitReaction[] hitReaction;
void Awake(){
GameObject[] reactors = GameObject.FindGameObjectsWithTag("HitReactor");
hitReaction = new HitReaction[reactors.Length];
for ( int i = 0; i < reactors.Length; ++i )
hitReaction[i] = reactors[i].GetComponent<HitReaction>();
}
Does GameObject.FindWithTag work on multiple objects?
@LeftyTwoGuns No, which is why I just added a 2nd implementation.
Ok ya, that partly answers my question. I should probably explain more what I'm doing. I have an asset that adds force to bones on a raycast hit. I need to reference that script (HitReaction) in my raycast in this script to do that. Here's how it currently works:
public HitReaction hitReaction;
void Awake(){
hitReaction = GameObject.FindGameObjectsWithTag("HitReactor").GetComponents<HitReaction>();
}
void DoHit(){
Ray ray = new Ray(m_Transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Range)
hitReaction.Hit(hit.collider, ray.direction * hitForce, hit.point);
}
Doing it that way, only one object will react to HitReaction because I'm assu$$anonymous$$g FindGameObjectWithTag is only returning one of them, which is why I figure I need FindGameObjectsWithTag
It's still unclear to me what you're trying to do, like why do you only have a single hitReaction stored for multiple hit reactors. Anyway, is this what you're going for?
public HitReaction[] hitReaction;
void Awake(){
GameObject[] reactors = GameObject.FindGameObjectsWithTag("HitReactor");
hitReaction = new HitReaction[reactors.Length];
for ( int i = 0; i < reactors.Length; ++i )
hitReaction[i] = reactors[i].GetComponent<HitReaction>();
}
void DoHit(){
Ray ray = new Ray(m_Transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Range) {
for ( int i = 0; i < hitReaction.Length; ++i )
hitReaction[i].Hit(hit.collider, ray.direction * hitForce, hit.point);
}
}
Oh wow, that works perfectly now. Thank you so much. Not only for helping me get it to work but also giving me a great example of how FindObjectsWithTag works because I need to use that again for other things in my project. Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613162054im_/https://answers.unity.com/themes/thub/images/avi.jpg)