Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

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

$
0
0
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!

Viewing all articles
Browse latest Browse all 171066

Trending Articles