- Home /
difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag)
i've been wondering what the difference between these 2 is. I've seen them both appear and they seem to do exactly the same. The best explaination i could come up with is that the code got rewritten and they didn't remove the old function. Also there is no information whatsoever on GameObject.FindGameObjectWithTag(Tag tag) in the referece manual.
Answer by jiteshvm · Apr 10, 2013 at 08:29 PM
they both do exactly the same thing.
not sure about the performance. I think its just the na$$anonymous$$g convention
Answer by Anxo · Oct 12, 2016 at 05:10 PM
I ran a performance test, 1000 cubes, one of them had the "Player" tag and ran the following code.
public class FindPerformanceTask : MonoBehaviour {
// Use this for initialization
void RunTest () {
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1000000; i++) {
GameObject player = GameObject.FindGameObjectWithTag("Player");
}
stopWatch.Stop();
Debug.Log(stopWatch.ElapsedMilliseconds.ToString()+"ms");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
RunTest();
}
}
}
The Science is in! Both perform the same.
Although it's there and works, there doesn't appear to be such a function as GameObject.FindGameObjectWithTag
in the API.
https://docs.unity3d.com/ScriptReference/GameObject.html
So, science notwithstanding, that suggests to me that one should prefer the documented FindWithTag
.
But does anyone know what's going on here? $$anonymous$$aybe they changed the name and left the old one as an alias for backwards-compatibility or something like that?
It is a well known fact the documentation on the site is often incomplete, inaccurate, or poorly written. The $$anonymous$$onoDevelop tooltips will at least verify usage parameters.
It is most likely the fact that "FindWithTag" has no logical plural ("FindsWithTag"?), but it would be rational to infer that FindObjects could reduce to FindObject.
Interesting. I wonder why Unity made a duplicate function with no documentation. 6 years after this question was asked, and the function is still there.
$$anonymous$$ake it 7 my friend. Welp, since there is no difference, I'll go with the shorter one.
Yep. As noted by @Bonfire-Boy, the shorter one is also documented so it definitely should be used instead.
Answer by Dracorat · Apr 10, 2013 at 08:35 PM
GameObject.FindGameObjectsWithTag
Finds a list of objects.
The other finds only one and returns it.
Yes. But there is also a function GameObject.FindGameObjectWithTag which only finds and returns one GameObject same as GameObject.FindWithTag