Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by rg16 · Sep 02, 2015 at 07:22 AM · raycastnullreferenceexception

How to account for a failed raycast?

So in my game, a user throws a gem onto a board, and it's supposed to match with other gems that are already on the board -- similar to bejeweled. When the gems match, they delete, and I have a series of raycasts look for gems above/below (depending on whose turn it is) to replace the deleted gems.

The problem is that if there's no gem for the raycast to hit, the script just stops execution and returns the error:

"NullReferenceException: Object reference not set to an instance of an object CamDragDetector+$ReplaceGems$65+$.MoveNext () (at Assets/Scripts/CamDragDetector.js:227)"

Example can be seen here: http://imgur.com/a/HdPWK

So, is there a way I could account for the lack of a gem to hit? My raycast outputs to hit3, so I tried using something like "if(!hit3.tranform)", but that doesn't seem to work.

Code:

 // gemsList is the list of matching gems
 for (var c = 0; c < gemsList.Count; c++) {        
     // Set up some variables...
     var origPos = gemsList[c].position;
     var nextPos : Vector3;
     var hit3 : RaycastHit;
     
     // Destroy a matching gem
     Destroy(gemsList[c].gameObject);
     
     // Now we need to shift all relevant gems in a direction
     do {
         // Change raycasts based on whose turn it is
         if (turnInt == 1) {
             raycast = Physics.Raycast(origPos, origPos.up, hit3); }
         else if (turnInt == 2) {
             raycast = Physics.Raycast(origPos, origPos.down, hit3); }
         
         if (raycast) {
             // The gems that move have to be tagged "BoardGem"
             if (hit3.transform.tag == "BoardGem") {
                 nextPos = hit3.transform.position;
                 hit3.transform.position = origPos;
                 origPos = nextPos;
             }
                 
             // This stores the position of the last BoardGem, for CreateGems() to initialize at that position
             missingPos.Clear();
             missingPos.Add(origPos);
         }
     }
     while (hit3.transform.tag == "BoardGem"); // This line is where the error hits
     
     // Create new gems as replacements
     CreateGems();
 }

Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by StaNov · Sep 02, 2015 at 08:03 AM

I guess you try to call hit3.transform, but hit3 is null.

So your condition should be

 if (!hit3 || !hit3.tranform) {...}

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 rg16 · Sep 03, 2015 at 04:06 AM 0
Share

Sorry for the late reply. I tried !hit3, but that just tells me that a RaycastHit can't return boolean values.

avatar image StaNov · Sep 03, 2015 at 06:12 AM 0
Share

@rg16 So try if (hit3 == null || hit3.tranform == null) {...}. Or simply just if (hit3 == null) {...} because non-null hit3 should always have it's transform assigned.

avatar image Positive7 · Sep 03, 2015 at 08:04 AM 0
Share
 if(hit3.collider || hit3.collider.transform){
                 Debug.Log(hit3.collider.name);
             }
avatar image rg16 · Sep 03, 2015 at 09:12 AM 0
Share

@StaNov and @Positive7 Actually, I just realized that the "if" might not have even worked (if I'm thinking this out right) because the error is ultimately hitting on the "while" line, which is a necessity. So when the hit3 updates, an "if" won't catch it when the condition is checked for in "while" (and I tried checking for a null hit3 IIRC).

This was a really hacky solution and I kind of hate it, but I had the game replace each gem a player uses with an invisible, unusable one -- these all delete when the player runs out of gems.

I appreciate the help though!

avatar image StaNov · Sep 03, 2015 at 09:49 AM 0
Share

$$anonymous$$y answer remains the same. I guess you are trying to access fields of hit3, but hit3 is null. So the while-line crashes, because you can't call null.transform. You should check whether hit3 is not null

 while (hit3 != null && hit3.transform.tag == "BoardGem");

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why am I getting a Null Reference Exception? 0 Answers

Raycast / Null Reference Exception 0 Answers

Null reference exception, even though I want my method to return null 0 Answers

Issue with 2D raycast 0 Answers

NullReferenceException while raycasting? 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