What am I doing wrong with this collision code?

public class BulletScript : MonoBehaviour {

	public GameObject hitEffect;

	private Transform myTransform;

	public float bulletSpeed = 0.1f;

	private bool spent = false;

	private RaycastHit hit;

	public float rayRange = 1.5f;



	// Use this for initialization
	void OnEnable () {

		myTransform = transform;

	}
	
	// Update is called once per frame
	void Update () {

		myTransform.Translate(Vector3.up * bulletSpeed * Time.deltaTime);

		if(Physics.Raycast(myTransform.position, myTransform.up, out hit, rayRange)&&
		   spent == false){

			if(hit.transform.tag == "Floor"){

				GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);

				Destroy(newHitEffect, 2);

				spent = true;

				GetComponent<PoolObject>().ReturnToPool();

			}

			spent = false;

		}else if(hit.transform.tag == "Target"){

			Destroy(this.gameObject);
		}
	
	}


}

Getting an error for the hit on the Target tag (line 46)

“Object reference not set to an instance of an object”

The projectile passes right through the cube tagged Target and I get that error. What did I do wrong here?

In your conditional at line 29, it is evaluating spent==false first, and not getting to the Physics.Raycast call. Your hit variable is uninitialised

The ELSE IF can execute if there is no collision, in which case “hit” is not populated. Just check the collision in the first IF, then use an “if (spent == false)” / “else if (hit.transform.tag == “Target”)” branch within the first IF.

if(Physics.Raycast(myTransform.position, myTransform.up, out hit, rayRange)) {
    if (spent == false){
        if(hit.transform.tag == "Floor"){
            GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);
            Destroy(newHitEffect, 2);
            spent = true;
            GetComponent<PoolObject>().ReturnToPool();
        }
        spent = false;
    } else if(hit.transform.tag == "Target"){
        Destroy(this.gameObject);
    }
}

The problem is with your placement of if…else loop.

Your else loop is for your if(Physics.Raycast...) loop which makes your hit object unavailable in your else loop since your hit object is for Raycast loop only.

I think your want to put your else loop for if(hit.transform.tag == "Floor"){ line of code.

The reason because hit is null. You tried to check hit.transform.tag while the raycast might not hit any object. I think you putted the else if in the wrong block. It should be after if(hit.transform.tag == "Floor") block which is:

            if(hit.transform.tag == "Floor"){
 
                GameObject newHitEffect = (GameObject)Instantiate(hitEffect, hit.point, Quaternion.identity);
 
                Destroy(newHitEffect, 2);
 
                spent = true;
 
                GetComponent<PoolObject>().ReturnToPool();
 
            }

            else if(hit.transform.tag == "Target"){
 
                Destroy(this.gameObject);
            }

Hope it’ll help.