NullReferenceException when trying to access parent
Hi,
I've got 2 "plane" objects in my scene:
is the visual part of a portal (with a mesh renderer, a mesh filter and a shader)
is a child of 1., has a box collider ("Is Trigger" is ticked) and a script (but no mesh renderer or mesh filter)
In the script I'm trying to get the name of the parent in OnTriggerEnter
with:
this.transform.parent.gameObject.name;
When I trigger this, it throws a:
NullReferenceException: Object reference not set to an instance of an object
this.transform.parent
isn't
null
(according to my print logs) but
this.transform.parent.gameObject
apparently is. Both planes exist at runtime, so how is that possible and what's causing this?
You will need to share more code than that. A GameObject can't exist without a Transform, and a Transform can't exist without a GameObject.
@Hellium Sorry for the late reply! There isn't more code to it though. Both GameObjects were created in Unity via "GameObject - 3D Object - Plane" and I then just dragged the second on top of the first, so it would be its child (which it is, according to the hierarchy). The script is attached to the child plane and in the OnTriggerEnter(Collider collider)
method I only do Debug.Log(this.transform.parent.gameObject.name);
- that's it. When I walk through the portal, this code throws said exception. The weird bit about this is that if I use this code in On$$anonymous$$ouseDown
instead, clicking on the portal actually displays the name - there's no exception unless I remove the this.
.