bool dropper = false;
RaycastHit2D hit = Physics2D.Raycast(new Vector2(x-1,y), -Vector2.up, y);
if (hit == null){
dropper = true;
}
else if (hit.transform.GetComponent().NormalType != NormalPieceType.Block){
dropper = true;
}
I’m getting a null reference exception pointing to the else if line. What’s going on here? It’s only supposed to go to that line if it’s not null.
It’s probably the object thats hit which not contains the following script: pieceControls which will trigger a nullpointer.
Try to place a Debug line with the name of the hit object so you know what was hit with the raycast.
You got something fundamentally wrong with Physics2D.Raycast. I admit that the way it works is a bit counter intuitive. Raycast will always return a RaycastHit2D. Since RaycastHit2D is a struct it can’t be null since it’s not a reference type.
Like you can see in the example of Physics2D.Raycast you should check if the collider field of the RaycastHit2D struct is null or not.