- Home /
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!
Answer by spencerj921 · Jan 30, 2014 at 09:14 PM
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.
Answer by supernat · Jan 30, 2014 at 08:42 PM
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.
Thanks for that efficiency note! $$anonymous$$y problem is that
obj.transform
is returning null, saying that obj (the object I collided with) is not set to any instance of an object, even though i set it equal to the col.gameObject
GameObject obj = col.gameObject;
Oh, I misunderstood. When something like that has happened to me, it usually means I did a "Destroy(this)" somewhere in my code ins$$anonymous$$d of "Destroy(gameObject)". Or I destroyed the result of transform.FindChild() and didn't realize I wasn't destroying the actual game object, so that gameobject still exists with a collider but no transform (possibly, though I can't remember if I've destroyed an actual transform recently). Do you destroy anything in the game?
I only have one line where I destroy enemies and i use
Destroy(gameObject)
this is confusing me, because this is all happening in the OnCollisionStay2D function, and theres no destroy function calls there.
That is confounding for sure. So, is col returning NULL or col.gameObject? All indications in the script ref are that col should always be valid, and if gameObject wasn't valid, then you wouldn't get the callback. This is likely a bug in Unity, this 2D stuff is pretty new still. You could maybe do a work around by setting a flag "inTriggerArea" true when get an OnTriggerEnter2D and false when you get the OnTriggerExit().
Having same problem here.. Really seems like it's bug in Unity.
I can't even do null check without null reference error so I had to return back to use 3D physics...
// Null check if gameobject is null -> throws null refence exception if (col.gameObject == null) return;