Raycast ignores Skybox

Hi, i am currently making an FPS in one week(Challenge) but i got stuck, the problem is that i made layers that the raycast ignores, and since the skybox is not an object, it ignores it. Help please.
Code: void Fire () { if (fireTimer < fireRate || currentBullets == 0) return; RaycastHit hit; if (Physics.Raycast(firePoint.position, transform.forward,out hit, Mathf.Infinity, myLayermask )) { Debug.Log("Hitted: " + hit.transform.name); } anim.CrossFadeInFixedTime("Fire", 0.01f); Rigidbody rb = hit.transform.GetComponent<Rigidbody>(); if(rb != null) { rb.AddForce(transform.forward * 700, ForceMode.Force); } currentBullets--; fireTimer = 0.0f; }

Hey, The problem is with this line here


Rigidbody rb = hit.transform.GetComponent();


After this line you check if the Rigidbody is null, but the problem is if you shoot the skybox, is the RaycastHit returned is null because it didn’t hit anything. A simple thing to do would just be to add an if statement before this like:


if (hit.transform == null) {
      // What to do if the bullet didn't hit anything
      return;
}

Note that return; will exit the void (will not execute the rest of the code below), so do anything you want to still do if the bullet didn’t hit anything, such as animation, sound etc, then return will exit the void, and avoid the errors you’re experiencing!


Hope that helps!