Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Transform.root not working.

Discussion in 'Scripting' started by Sandesh321, Sep 18, 2021.

  1. Sandesh321

    Sandesh321

    Joined:
    May 2, 2021
    Posts:
    53
    Hey there I'm trying to make a fps game. But when I hit an enemy, sometimes I hit the child collider (of an arm for ex, but the components I need aren't sitting there) and can't return the components I need. So I tried to use transform.root to get the highest in hierarchy where the components are sitting. For some reason, the components still aren't referenced. Can some one help? It says "code not found" (my debugging).
    Code (CSharp):
    1.   //checking if its a structure
    2.             GameObject parent = hit.transform.root.gameObject; // tried accessing the parent first, still doesn't work
    3.             StructureHealth structure = parent.GetComponent<StructureHealth>();
    4. // StructureHealth structure = hit.transform.root.GetComponent<StructureHealth>(); doesn't work either
    5.             ZombieAiReWork zombieAi = parent.GetComponent<ZombieAiReWork>();
    6.             ZombieWarn warnOthers = parent.GetComponent<ZombieWarn>();
    7.             ZombieHealth zombieHealth = parent.GetComponent<ZombieHealth>();
    8.            
    9.  
    10.             if (structure != null)
    11.             {
    12.                 print(" found");
    13.                 structure.TakeDamage(Damage);
    14.             }
    15.             if (zombieAi != null)
    16.             {
    17.                 print(" found");
    18.                 print("damage taken");
    19.                 zombieAi.IsAgro = true;
    20.                 zombieHealth.TakeDamage(Damage);
    21.             }
    22.             else
    23.             {
    24.                 print("code not found"); //returns this
    25.             }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Use GetComponentInParent on the hit.transform and it'll get it from the transform or any of it's parents all the way up the hierarchy automatically.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    Are you actually sure about that? Transform.root returns the absolute topmost gameobject. Are you sure that the object you hit is not a child of another object? Because, as just said, the root property returns the very top most gameobject, not just the topmost of your prefab or what you consider your enemy. Since we have no idea how your hierarchy actually looks like we can not really tell you want went wrong. You just said the components are on the top most object. If they are, your code should work. However if they are not, of course it won't work. As GroZZleR said, GetComponentInParent is usually the easiest solution as it goes up the hierarchy, level by level and returns the first component it comes across.
     
  4. Sandesh321

    Sandesh321

    Joined:
    May 2, 2021
    Posts:
    53

    The hierarchy looks like :
    enemy: (has component and a collider)
    > Body (has collider for ragdoll effect)
    >> chest (has collider for ragdoll effect)
    >>> spine (has collider for ragdoll effect)
    > L arm (has collider for ragdoll effect)
    >> forearm and palm (has collider for ragdoll effect)
    > R arm (has collider for ragdoll effect)
    >> forearm and palm (has collider for ragdoll effect)
    > Legs (has collider for ragdoll effect)
    >> foot & leg (has collider for ragdoll effect)

    I think the bullets might be hitting the foot or forearm and trying to get component from the Arms or Legs