NullReferenceException when i try to access the transform of the object i collided with

void OnCollisionStay2D (Collision2D col)
{
GameObject obj = col.gameObject;
if (!hasWeapon) {
if (checkIfPickUp (obj)) {
pickUpObject (obj);
}
}
}
bool checkIfPickUp (GameObject obj)
{
if (obj.tag == “rock”
|| obj.tag == “spinner”
|| obj.tag == “sword”
|| obj.tag == “smallGun”
|| obj.tag == “bigGun”) {
weaponTag = obj.tag;
//print ("weaponTag = " + weaponTag);
return true;
}
return false;
}
void pickUpObject (GameObject obj)
{
obj.transform.position = transform.FindChild (weaponTag).position;
obj.transform.rotation = transform.FindChild (weaponTag).rotation;
obj.collider2D.isTrigger = true;
obj.rigidbody2D.gravityScale = 0;
obj.rigidbody2D.velocity = new Vector2 (0, 0);
obj.rigidbody2D.isKinematic = false;
obj.rigidbody2D.fixedAngle = false;
hasWeapon = true;
currWeapon = obj;

		}

i always get the error wherever i put this first line where i want to access the transform.

obj.transform.position = transform.FindChild (weaponTag).position;

unless i put it as the first line in OnCollisionStay2D outside of the if statements.
Can anyone see what I cant? Thanks!

You need to do the operation in two steps. Don’t rely on the other object being a weapon or having a weapon tag (maybe you forget to assign the tag in the editor?)

Transform otherObj = transform.FindChild(weaponTag);

// Always validate otherObj
if (otherObj)
    obj.transform.position = otherObj.position;

Also, note that FindChild is an iterative process of comparing strings and can be inefficient. You’re calling it twice in pickUpObject, so you might as well assign it as shown here anyway so it is more efficient.

Alright, sorry, the child i wanted to access was actually the child of a child of the parent transform. So i needed to use FindChild() twice.