- Home /
nullreference exception despite it being attached to the named object??
I am receiving this error:
NullReferenceException: Object reference not set to an instance of an object triggerZone.OnTriggerEnter (UnityEngine.Collider Fireboys) (at Assets/scripts/triggerZone.cs:23)
However, 'Fireboys' is set to 'badcampfire', as seen here:
void OnTriggerEnter(Collider Fireboys){
if(Fireboys.gameObject.tag == "Player"){
transform.Find("badcampfire").SendMessage("FireCheck");
}
}
The object, 'badcampfire', is named appropriately, as seen here:
So, yeah, I'm at an impasse. What have I done wrong, or, alternatively, what can I do to show off to you guys what I did wrong?
Answer by Pharan · Oct 05, 2015 at 06:22 PM
Documentation for Transform.Find says:
Finds a child by name and returns it.
If no child with name can be found, null is returned. If name contains a '/' character it will traverse the hierarchy like a path name.
Is "badcampfire" a child transform of the GameObject this script is on? If not, that's probably why it's returning null, as the documentation says.
Just a hunch.
On a related note, it's probably easier to just get a reference to the script that has the "FireCheck" method and then call it directly. Or maybe it's not...