Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spencerj921 · Jan 30, 2014 at 08:33 PM · collisiontransformnullreferenceexceptioninstance

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spencerj921 · Jan 30, 2014 at 08:55 PM 0
Share

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;

avatar image supernat · Jan 30, 2014 at 08:58 PM 0
Share

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?

avatar image spencerj921 · Jan 30, 2014 at 09:05 PM 0
Share

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.

avatar image supernat · Jan 30, 2014 at 09:18 PM 0
Share

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().

avatar image Stactic · Apr 02, 2014 at 12:20 PM 0
Share

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;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision between two moving clones 0 Answers

Why does this Instantiate give a Null Reference Exception? 1 Answer

NullReferenceException when using raycast 1 Answer

move camera when it collides with a trigger 1 Answer

Transform.Position Collision Issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges