Hello everyone, I’m back at it with my previous project. I am creating a laser that detects the target and changes the background colour to red, when it doesn’t it returns to black (default background)

The problem I’m encountering is that the laser detects everything as the ‘target’. However, it works when I position the laser onto the background and turns black.

Here’s my code:

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

public class ColorScript : MonoBehaviour
{
    [SerializeField] Transform laserSpawn;
    [SerializeField] LineRenderer laser;

    [SerializeField] GameObject target;

    [SerializeField] GameObject environment;


    //public float range = 100;

    [SerializeField] Camera camFps;
    //private Color camRed = Color.red;

    // Use this for initialization
    void Start()
    {
        camFps = GetComponent<Camera>();
        laser.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        //Right mouse, controls laser sight functionality
        if (Input.GetMouseButton(1))
        {
            laser.enabled = true;
            laser.SetPosition(0, laserSpawn.position);
            laser.SetPosition(1, laserSpawn.position + laserSpawn.forward * 50);
            RaycastHit hit;
            Vector3 fwd = laserSpawn.TransformDirection(Vector3.forward);
            Debug.DrawRay(laserSpawn.position, fwd * 20, Color.green);

           //When laser detects target turn red,. If not, black.
            if (Physics.Raycast(laserSpawn.position, fwd, out hit) && target == true)
            {                
                Debug.Log("Target Detected");
                laser.SetPosition(1, hit.point);                
                camFps.backgroundColor = Color.red;
            }
            else
            {
                Debug.Log("Target Not Detected");
                camFps.backgroundColor = Color.black;     
            }
        }

        //When release right mouse click, reset laser.enabled to false
        if (Input.GetMouseButtonUp(1))
        {
            laser.enabled = false;
        }
    }
}

This script is placed under my FirstPersonCharacter parent

I’ve toke some screenshots if anyone wants more detail:

Target detected:

Target not detected:

Thanks everybody.

Hi, I think you forgot to check for a tag. With (Physics.Raycast(laserSpawn.position, fwd, out hit) you check if the Raycast detects a collider. So all GameObjects with a collider will enable the condition. you should check if the tag of the hitted collider is the Target like this:

            if (Physics.Raycast(laserSpawn.position, fwd, out hit) && target == true)
            {
                if (hit.collider.tag == "Target")
                {
                    Debug.Log("Target Detected");
                    laser.SetPosition(1, hit.point);
                    camFps.backgroundColor = Color.red;
                }
                else
                {
                    Debug.Log("Object Detected But Target Not Detected");
                    laser.SetPosition(1, hit.point);
                    camFps.backgroundColor = Color.black;
                }
            }
            else
            {
                Debug.Log("Target Not Detected");
                camFps.backgroundColor = Color.black;
            }