Search Unity

GetComponentInParent

Discussion in 'Scripting' started by topofsteel, Nov 12, 2015.

  1. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    975
    I'm trying to get the parent transform of a collider. The asset has the collider burred in the hierarchy. I want to move the prefab (a car in this case) to another part of the scene. I'm getting the error 'object not set to an instance...' It's not getting the transform. How do I do this?


    Code (CSharp):
    1. public class CarTeleport : MonoBehaviour
    2. {
    3.     public Transform Destination;
    4.     private VehicleController vehicleControler = null;
    5.  
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         vehicleControler = other.GetComponent<VehicleController>();
    9.         vehicleControler.GetComponentInParent<Rigidbody>().transform.position = Destination.transform.position;
    10.     }
    11. }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    If you want the transform, use transform.parent.position instead
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,163
    If your rigidbody is multiple levels up in the hierarchy from where this script is - e.g. it's in the grandparent, or the great-grandparent - GetComponentInParent won't find it. You'll have to do some "crawling".
    Code (csharp):
    1. Rigidbody ancestorRigidbody = null;
    2. Transform thisParent = transform;
    3. while (thisParent != null && ancestorRigidbody == null) {
    4. ancestorRigidbody = thisParent.GetComponent<Rigidbody>();
    5. thisParent = thisParent.parent;
    6. }
    After this, ancestorRigidbody will be filled with the first rigidbody it finds, if any.
     
  4. jmjd

    jmjd

    Joined:
    Nov 14, 2012
    Posts:
    43
    You shouldn't have to do your own search, that's what GetComponentInParent() does, and the docs confirm this.

    Your error, "object not set to an instance..." leads me to believe the problem isn't with the GetComponentInParent call. I would expect a null exception error if it wasn't finding the parent rigidbody.

    If I had to guess, it would be you assigned a prefab to the 'Destination' value in the inspector. So you are trying to use the prefab, instead of an instance of the prefab.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,163
    ....huh, has it always done that? I could swear that in both experimentation and docs it had only looked at the immediate parent. (which is also what the function name describes...)
     
  6. jmjd

    jmjd

    Joined:
    Nov 14, 2012
    Posts:
    43
    I don't know if it's new or not, but I only became aware of it recently and it did search up recursively. But I was surprised when I found it, since I had previously used your method.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,629
  8. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    975
    My error was actually in line 8, that's where I needed to use 'GetComponentInParent', not line 9. It does in fact do the searching for you. Thank you for your replies.
     
unityunity