access grandfather of an object

hi.i have 4 object a ,b ,c ,d.
d is child of c.
c is child of b.
b is child of a.
i want to access the script attached to a gameobject in d gameobject script without tag and just with parent relationship.can you help me?

gameObject.transform.parent.parent

the parent returns a transform type.

One of theses will probably work:

gameObject.transform.parent.parent

Or

gameObject.transform.parent.transform.parent

Or

gameObject.transform.parent.gameObject.transform.parent

I haven’t triad any of them, so maybe some will work, maybe all or maybe non.

Of course, you can keep going on and on and on:

gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.parent.....

If you want to get the top level ancestor of a a gameobject there’s a couple ways to do it. While this isn’t exactly what you asked for I think it can help. A while loop or recursion. First is a while loop then a recursive function.

GameObject parent = transform.parent;
while(parent.transform.parent) { // This checks if it's null or not.
    parent = parent.transform.parent;
}

Below is a recursive function

GameObject getAncestor(GameObject item) {
     if(item.transform.parent == null)
          return item;
     getAncestor(item.transform.parent);
}

I didn’t have a chance to test these but the idea is there.