Why gameObject copy can not do the script commands?

Hi, I’m making 2D laser system and when the player collides with the ray it gets the damage.

So it’s works fine, but in the first laser, every other copy is just showing the error about tag of the player.

Here is the error line:

if (hit.collider.tag == "Player" || spottedLeft == true)

Here is full script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OneLaserScript : MonoBehaviour {

	private LineRenderer lineRenderer;

	public Transform laserHit;

	public GameObject player;

	public Transform leftCheck, rightCheck;

	public Transform leftHit, rightHit;

	public bool spottedLeft, spottedRight;

	public LayerMask detectionLayers;

	void Start () {
		lineRenderer = GetComponent<LineRenderer> ();
	void Update () {
		RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.up);
		laserHit.position = hit.point;
		lineRenderer.SetPosition (0, transform.position);
		lineRenderer.SetPosition (1, laserHit.position);

		Debug.DrawLine (leftCheck.position, leftHit.position, Color.green);
		spottedLeft = Physics2D.Linecast (leftCheck.position, leftHit.position, detectionLayers);

		Debug.DrawLine (rightCheck.position, rightHit.position, Color.green);
		spottedRight = Physics2D.Linecast (rightCheck.position, rightHit.position, detectionLayers);

		if (hit.collider.tag == "Player" || spottedLeft == true) {
			player.GetComponent<PlayerControllerScript> ().knockFromRight = true;
			player.GetComponent<PlayerControllerScript> ().knockbackCount += 1;
			player.GetComponent<PlayerControllerScript> ().timer = 5f;
		} else {
			player.GetComponent<PlayerControllerScript> ().knockFromRight = false;
			player.GetComponent<PlayerControllerScript> ().knockbackCount = 0;
		if (hit.collider.tag == "Player" || spottedRight == true) {
			player.GetComponent<PlayerControllerScript> ().knockbackCount += 1;
			player.GetComponent<PlayerControllerScript> ().timer = 5f;
		} else {
			player.GetComponent<PlayerControllerScript> ().knockbackCount = 0;

What I have done wrong? Please try to help.

UPDATE: indeed the error is that the raycast didn’t hit anything, so hit.collider is null, so trying to access hit.collider.tag throws a NullReferenceException. Always check the result of raycasts, either by using if (hit.collider != null) or using the version that returns a bool and puts the result in an out parameter.

Original post:
What is the exact error it says?

One possible issue is that you don’t check if the raycast hit something, you just use hit.point. However, if nothing was hit, hit.collider will be null (see documentation here) and you will get an error when you try to access the tag member of it.

Did this solve the problem?