The first two items are normal behaviour. You can cast a Vector3 to a Vector2, unity will simply drop the z component. Transform.position is a Vector3.
Not sure whatâs happening in the second example.
Unitys transform component is the same for 2D and 3D objects, so every object has a vector3 as its transform position.
for the second one, iâm not sure but i think one of unityâs ways to access child-transforms is âTransform in transformâ (like foreach (Transform t in transform) {t.doSomething();} ), so youâre actually re-accessing the child.
GetComponentInParent returns the component attached to the given GameObject or any of its parents. Every GameObject has a transform so in that context youâll always get back the transform of the GameObject youâre looking at. I would imagine in your second case the parent has a Rigidbody2D component but the GameObject with the collider does not.
In your first example - transform.position is always a Vector3 regardless of how you get to it. Rigidbody2D.position is a Vector2 because it only deals in 2 dimensions.
I always thought parent was a reference to the hierarchy, not âclosest object above with this game Componentâ.
For some reason, suddenly, I have many things to fix.
There is your answer. I personally never use GetComponentInParent. As a general rule my GameObjects are not allowed to know about their parents, except in a very general SendMessageUpwards or ExecuteEvents.ExecuteHeirarchy kind of way.
One way dependency is much easier to manage. (ie parents can know about their children, but children cannot know about their parents).