Change Layer On Raycast Hit Problem Help Please

Hi i have a method that change layers like down below. With layer change i change the color of object with shader.

 private void SetLayerRecursively(GameObject obj, int newLayer)
    {
        if (null == obj)
        {
            return;
        }

        obj.layer = newLayer;

        foreach (Transform child in obj.transform)
        {
            if (null == child)
            {
                continue;
            }
            SetLayerRecursively(child.gameObject, newLayer);
        }
    }

And i have a code that raycast controlling which object in the mouse position like down below.

private void RaycastHitCheck()
    {
        Ray CameraRay = PlayerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        //Raycast Hit Pickup and Highlight Layers
        if (Physics.Raycast(CameraRay, out RaycastHit HitInfo, PickupRange, LayerMask.GetMask("PickUp", "Highlight")))
        {
            CurrentObjectNoTake = HitInfo.rigidbody;

            if (Input.GetKeyDown(KeyCode.E) && !isObjectInHands && CurrentObjectForPlace == null)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
                isObjectInHands = true;
            }
            if (Input.GetMouseButtonDown(0) && !isObjectInHands)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
            }
            //Raycast Hit But Dont Press E
            if (!isObjectInHands && CurrentObjectNoTake != null)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, highlightMask);
            }
        }
        //Raycast Doesn't Hits Anything
        else
        {
            if (CurrentObjectNoTake != null)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
            }
        }
    }

I deleted the unnecessary code so you can see it better thats why there is a lot of ifs.

The problem is
when raycast hit object outside pickup layer there is no problem but when raycast hit pickup to pickup object again both of them staying highlighted color
Like you see in the video

Can’t you just check if CurrentObjectNoTake changes at all, and reset the old one before changing it to the new HitInfo.rigidbody?

1 Like

If you mean this code sir its not working

          if (CurrentObjectNoTake != HitInfo.rigidbody)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
            }

or

          if (CurrentObjectNoTake != CurrentObjectNoTake)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
            }

this too

How can i check beside that

What does it look like with the rest of the code? I can’t be sure that you didn’t just implement it incorrectly.

1 Like

Ohhh… when i write above hitinfo like below it worked. I wasted too much time on this im idiot :smile: Thanks for helps sir

 if (Physics.Raycast(CameraRay, out RaycastHit HitInfo, PickupRange, LayerMask.GetMask("PickUp", "Highlight")))
        {
            if (CurrentObjectNoTake != HitInfo.rigidbody && CurrentObjectNoTake!= null)
            {
                SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
            }
            CurrentObjectNoTake = HitInfo.rigidbody;
...