Fade in Out Object

I guys i hv this piece of code that fades an object between camera and my player my only problem is that when i leave the object its not returning to normal color.

can anyone help me plz?

        if(fadeObjects)
        {       
            // Cast ray from camera.position to target.position and check if the specified layers are between them.
            Ray ray = new Ray(myTransform.position, (target.position - myTransform.position).normalized);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, maxDistance)) {
                Transform objectHit = hit.transform;
                if(layersToTransparent.Contains(objectHit.gameObject.layer))
                {
                    if(prevHit!=null)
                    {
                        prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                    }
                    if(objectHit.GetComponent<Renderer>() != null)
                    {
                        prevHit = objectHit;
                        // Can only apply alpha if this material shader is transparent.
                        prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,alpha);
                    }
                }           
                else if(prevHit != null)
                {
                    prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                    prevHit = null;
                }
            }
        }

Your else if is on the wrong level. Put another } right before it, and delete one of the ones after it.

I change it but keeps not going back to full color

if(fadeObjects)
        {       
            // Cast ray from camera.position to target.position and check if the specified layers are between them.
            Ray ray = new Ray(myTransform.position, (target.position - myTransform.position).normalized);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, maxDistance))
            {
                Transform objectHit = hit.transform;
                if(layersToTransparent.Contains(objectHit.gameObject.layer))
                {
                    if(prevHit!=null)
                    {
                        prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                    }
                    if(objectHit.GetComponent<Renderer>() != null)
                    {
                        prevHit = objectHit;
                        // Can only apply alpha if this material shader is transparent.
                        prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,alpha);
                    }
                }
            }
            else if(prevHit != null)
            {
                prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                prevHit = null;
            }
        }

I spoke too soon, it looks like you need both- if I’m reading this right. Here are the requirements, as I assume them to be:

  • If a raycast isn’t hitting anything, then set the previously hit object, if one exists, to normal opacity, then assign it null.
  • If the raycast is hitting something, but it’s the wrong layer, then set the previously hit object to normal opacity, then assign it null.
  • If the raycast is hitting something on the right layer, and it’s a different object than last time, then set the previously hit object to normal opacity, then assign it null.
  • If the newly hit object has a renderer, set it to altered opacity, then set the newly hit object to be the previously hit object.

You’ll need both the original placement of the else if, and the new placement, and you need to add “&& prevHit != objectHit” to the “if(prevHit!=null)” conditional in the middle there, I think, and add a null assignment after.

if(fadeObjects)
{
    Ray ray = new Ray(myTransform.position, (target.position - myTransform.position).normalized);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, maxDistance))
    {
        Transform objectHit = hit.transform;
        if(objectHit != prevHit)
        {
            if(layersToTransparent.Contains(objectHit.gameObject.layer))
            {
                if(prevHit != null)
                {
                    prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                    prevHit = null;
                }
                if(objectHit.GetComponent<Renderer>() != null)
                {
                    prevHit = objectHit;
                    prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,alpha);
                }
            }
            else if(prevHit != null)
            {
                prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
                prevHit = null;
            }
        }
    }
    else if(prevHit != null)
    {
        prevHit.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
        prevHit = null;
    }
}

There’s probably a far more efficient way of doing this, but I’m too tired just now to try. Good luck!