Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
How can I disable a component once a raycast is no longer on an object?

I have a raycast that enables an outline component if the object has one, but I want it to be that when the raycast is no longer on that object the component is disabled. I keep on getting errors with my attempted solutions. Here is my code, the stuff about the text is not realated to my problem:

private void ShootRaycast()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, viewRange))
        {
            if (hit.transform.name == "Player")
            {
                objectText.text = "Object: Ground";
            }
            else
            {
                objectText.text = "Object: " + hit.transform.name;
            }

            if (hit.transform.GetComponent<Outline>())
            {
                hit.transform.GetComponent<Outline>().enabled = true;
            }
        }
        else
        {
            objectText.text = "Object: None";
        }
    }

Thanks for any help.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I assume you are calling that method e.g in Update.

You could simply keep a reference to the last hit object and disable it like e.g.

private Outline _currentOutline;

private void ShootRaycast()
{
    if (Physics.Raycast(transform.position, transform.forward, out var hit, viewRange))
    {
        if (hit.transform.name.Equals("Player"))
        {
            objectText.text = "Object: Ground";
        }
        else
        {
            objectText.text = "Object: " + hit.transform.name;
        }

        // Note: Use TryGetComponent to avoid repeated usage of GetComponent
        if (hit.transform.TryGetComponent<Outline>(var outline))
        {
            // You are hitting a new object
            if(outline != _currentOutline)
            {
                // If you had enabled something else before, disable it first
                if(_curtentOutline) 
                {
                    _currentOutline.enabled = false;
                }

                // Enable and store the current hit outline
                outline.enabled = true;
                _currentOutline = outline;
            }
        }
        // Hitting something that has no outline component
        else
        {
            // if there was a current outline
            if(_currentOutline)
            {
                // Disable the current outline and forget the reference
                _currentOultine.enabled = false;
                _currentOutline = null;
            }
        }
    }
    // There is no hit at all
    else
    {
        objectText.text = "Object: None";

        // If there was a current outline
        if(_currentOutline)
        {
           // Disable it and forget the reference
            _currentOultine.enabled = false;
            _currentOutline = null;
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!