Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

517
Visualizações
Any difference between Component.CompareTag() and GameObject.CompreTag()?

I'd like to know if there's any under-the-hood difference in the execution of these two CompareTag() calls:

private void OnTriggerEnter(Collider otherCollider)
{
    if (otherCollider.CompareTag("TestTag"))
    {
        Debug.Log("The other Collider is tagged: TestTag");
    }

    if (otherCollider.gameObject.CompareTag("TestTag"))
    {
        Debug.Log("The other GameObject is tagged: TestTag");
    }
}

I suspect they should be identical – a Component is always attached to one and only one GameObject, so it shouldn't be necessary to access the Component's ".gameObject" reference first. Both calls behave identically as far as I can tell.

I wonder because:

  1. Calling otherCollider.gameObject.CompareTag() doesn't result in a “name can be simplified” warning. This could be because those classes and methods are in the UnityEngine namespace, and thus not covered by VisualStudio's warnings?
  2. The example in the Unity documentation for Component.CompareTag() explicitly uses the longer form of the call: other.gameObject.CompareTag("Player"). This could just be the result of a sloppy copy-paste from the GameObject.CompareTag() method's documentation, which provides an identical example? https://docs.unity3d.com/ScriptReference/Component.CompareTag.html
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you take a look at the actual code for Component.CompareTag(), you'll see this:

    // UnityEngine.Component
    /// <summary>
    ///   <para>Is this game object tagged with tag ?</para>
    /// </summary>
    /// <param name="tag">The tag to compare.</param>
    public bool CompareTag(string tag)
    {
        return gameObject.CompareTag(tag);
    }

So, if you want to get down to the "under the hood" implementation, at a micro-optimisation level, when you call Component.CompareTag(), you're ALSO calling the gameObject property as well, which has it's own processing time associated with accessing it. Now, that processing time isn't huge, but it IS there because properties have similar processing overheads to method calls, as they're essentially the same.

The process then goes Component.CompareTag() -> Component.gameObject{get} -> GameObject.CompareTag().

So, the long and short of it is, that if you can call a method further down the call stack, instead of a top level helper method, you're best to chose the more performant one - GameObject.CompareTag().

As for the example you gave, it's essentially saving one method call, by accessing the Component gameObject property directly.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda