Raycast Works in editor, but not wen you build it!! Help please!! Cs

Hey,
So, Wen i run the game in the editor it works perfectly, but wen i have build the game it the raycast doen nothing at all and doesn’t work
Is this a Bug?
Or is it just my script?

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

public class InteractScript : MonoBehaviour {

    public float InteractDistance = 5;
    public KeyCode InteractKey;
    public GameObject InteractObject;
    public bool InteractObjeState = false;

    RaycastHit hit;

    void Start()
    {
    
    }

    void Update ()
    {
//-----------------------------------------------------------------------------

        if (InteractObjeState == false)
        {
            InteractObject.SetActive(false);
        }

        if (InteractObjeState == true)
        {
            InteractObject.SetActive(true);
        }
//-----------------------------------------------------------------------------
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit, InteractDistance))
        {
            if(hit.collider.CompareTag("LootAble"))
            {
                InteractObjeState = true;

                if (Input.GetKeyDown(InteractKey))
                {
                    hit.collider.gameObject.GetComponent<ItemId>().Enter();
                    InteractObjeState = false;
                }
            }
            else
            {
                InteractObjeState = false;
            }
        }
//-----------------------------------------------------------------------------
    }
}

All Help is Welcome!

Try Physics.Linecast instead. And don’t use game tags for hit check…it generates overhead. Use Interface or MonoBehaviour scrit check instead.

        if (Physics.Linecast(transform.position, transform.forward))
        {
            if(hit.collider.CompareTag("LootAble"))
            {
                InteractObjeState = true;

                if (Input.GetKeyDown(InteractKey))
                {
                    hit.collider.gameObject.GetComponent<ItemId>().Enter();
                    InteractObjeState = false;
                }
            }
            else
            {
                InteractObjeState = false;
            }
        }

i got know this but the rest i don’t understand:face_with_spiral_eyes:

instead of

if(hit.collider.CompareTag("LootAble"))

// You should use script

if (hit.collider.GetComponent<LootAble>() != null )