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!
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) {...}
Sorry for the late reply. I tried !hit3, but that just tells me that a RaycastHit can't return boolean values.
@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.
if(hit3.collider || hit3.collider.transform){
Debug.Log(hit3.collider.name);
}
@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!
$$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
Follow this Question
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