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 Kastenessen · Jan 23, 2015 at 10:17 PM · 2draycastdestroyplatformernull reference exception

Platfomer 2d game block destroying raycasting nullreference exception problem

Hi, I need a bit of help.

I generate a level and have destroyable blocks in it as well as ai characters that are set up with raycasts onto the solids layer to check different things, like if there is a platform next to them then they can walk left or if not walk right.

I've since added the ability to destroy the solids. (platforms and wall blocks).

I get null reference exceptions now from the characters raycasts once a blocks been destroyed (I'm pretty sure that's whats causing it). I've tried real hard to solve this but now don't know what to do. My raycast script is:

     isPlatLeft = Physics2D.Raycast (new Vector2 (transform.position.x - 1.5f, transform.position.y), new Vector2 (-0.5f, -1), 8f, moveMask);
                                                 


                         

I've got just the walls and platforms and ladders set on the moveMask.

This is where I get the nullreference error:

 // check if a platform is under the player and off to the left.
                                         if (laddCheckLeft) {
                                                 if (laddCheckLeft.collider.tag != null) {
                                                         if (laddCheckLeft.transform.tag == "PLATFORM" || laddCheckLeft.transform.tag == "LaddCollider" || laddCheckLeft.transform.tag == "SOLID") { 
                                                                 //    int rane = Random.Range (0, 40);
                                                                 nowOffLadder = false;
                                                                 //if (rane == 1) {
                                                                 laddPlatToLeft = true;
                                                                 //}
                                                         } else {
                                                                 wasOnLadder = true;
                                                                 laddPlatToLeft = false;
                                                         }
                                                 } else {
                                                         wasOnLadder = true;
                                                         laddPlatToLeft = false;
                                                 }
                                         } else {
                                                 wasOnLadder = true;
                                                 laddPlatToLeft = false;
                                         }
             


Everything worked fine until I started destroying blocks. As you can see I've tried to stop it from happening but not sure what to do now and whats going on.

Can someone help PLEASE???

Comment
Add comment · Show 8
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 InvincibleCat · Jan 23, 2015 at 10:22 PM 0
Share

Which line causes the NullReferenceException ?

(please, when you post a code, try to have a correct indentation and remove commented code if it is useless)

avatar image Kastenessen · Jan 23, 2015 at 10:28 PM 0
Share

This does:

if (laddCheckLeft.transform.tag == "PLATFOR$$anonymous$$" || laddCheckLeft.transform.tag == "LaddCollider" || laddCheckLeft.transform.tag == "SOLID") {

avatar image Kastenessen · Jan 23, 2015 at 10:29 PM 0
Share

Sorry bout the comments, but what do you mean about correct indentation, I pasted it as is from my code lol.

avatar image InvincibleCat · Jan 23, 2015 at 10:30 PM 0
Share

and what is laddCheckLeft ? can't see where it is initialized

avatar image InvincibleCat · Jan 23, 2015 at 10:31 PM 0
Share

I mean that by correct identation

     if (laddCheckLeft)
     {
         if (laddCheckLeft.collider.tag != null)
         {
             if (laddCheckLeft.transform.tag == "PLATFOR$$anonymous$$" || 
                 laddCheckLeft.transform.tag == "LaddCollider" || 
                 laddCheckLeft.transform.tag == "SOLID")
             {
                 //    int rane = Random.Range (0, 40);
                 nowOffLadder = false;
                 //if (rane == 1) {
                 laddPlatToLeft = true;
                 //}
             }
             else
             {
                 wasOnLadder = true;
                 laddPlatToLeft = false;
             }
         }
         else
         {
             wasOnLadder = true;
             laddPlatToLeft = false;
         }
     }
     else
     {
         wasOnLadder = true;
         laddPlatToLeft = false;
     }
Show more comments

1 Reply

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

Answer by InvincibleCat · Jan 23, 2015 at 10:46 PM

This should work

         if (laddCheckLeft)
         {
             Collider2D collider = laddCheckLeft.collider;
             if(collider != null)
             {
                 string tag = laddCheckLeft.collider.tag;
                 if (tag == "PLATFORM" ||
                     tag == "LaddCollider" ||
                     ltag == "SOLID")
                 {
                     //    int rane = Random.Range (0, 40);
                     nowOffLadder = false;
                     //if (rane == 1) {
                     laddPlatToLeft = true;
                     //}
                 }
                 else
                 {
                     wasOnLadder = true;
                     laddPlatToLeft = false;
                 }
             }
             else
             {
                 wasOnLadder = true;
                 laddPlatToLeft = false;
             }
         }
         else
         {
             wasOnLadder = true;
             laddPlatToLeft = false;
         }
Comment
Add comment · Show 8 · 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 InvincibleCat · Jan 23, 2015 at 11:03 PM 0
Share

Does it work???

avatar image Kastenessen · Jan 23, 2015 at 11:12 PM 0
Share

Thanks, it seems to work. Would have taken me ages to work that out by myself, I've not been coding that long. Awesome so I can have destroyable blocks coolies. Thanks again!

avatar image InvincibleCat · Jan 23, 2015 at 11:14 PM 0
Share

You're welcome ;) Don't forget to accept the answer!

avatar image Kastenessen · Jan 23, 2015 at 11:16 PM 0
Share

I'll test it a bit more. I had to replace several code parts with the new script so it took me a few $$anonymous$$utes. But it seems to work. I have randomly generated levels that generate platforms and ladders and rooms in different directions and I want the characters ai to be able to do what you can, and so far its going really well, thanks to my perseverance and the wonderful people here on Unity Answers.

avatar image InvincibleCat · Jan 23, 2015 at 11:20 PM 0
Share

Glad that you found the solution.

Please don't forget to accept the answer. I spend to many time going after people asking them to accept so I can sort the questions I'm answering. And it is a real waste of time :(

Show more comments

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

How to determine if the player can jump, without using raycasts. (2D) 1 Answer

Using RayCasting to check for floor and ceiling? 0 Answers

Player sinking into walls and floor with Raycasts 1 Answer

2D Slope acceleration 1 Answer

Ways to find bounds of the platform 1 Answer


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