At the line of
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) {
I get the
Object reference not set to an instance of an object
? I have tried a lot of things and still don’t understand why my Raycast Hit does not work please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript1 : MonoBehaviour
{
public float MaxDist, DMG;
public Transform FX;
public LayerMask PlayerMask;
void OnCollisionEnter(Collision collision)
{
Collider[] Enemies = Physics.OverlapSphere(transform.position, MaxDist, PlayerMask);
foreach (Collider t in Enemies) {
RaycastHit hit;
Physics.Raycast(transform.position, new Vector3 (Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)), out hit, MaxDist);
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) {
var HPScript = t.GetComponent<Target>();
if (HPScript) {
HPScript.TakeDamage(DMG);
Debug.Log("Hit somethin");
}
}
}
Instantiate(FX, transform.position, transform.rotation);
Object.Destroy(transform.gameObject);
}
}