Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
¿Cómo puedo deshabilitar un componente una vez que un raycast ya no está en un objeto?

Tengo un raycast que habilita un componente de contorno si el objeto tiene uno, pero quiero que cuando el raycast ya no esté en ese objeto, el componente esté deshabilitado. Sigo recibiendo errores con mis intentos de solución. Aquí está mi código, las cosas sobre el texto no están relacionadas con mi problema:

 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"; } }

Gracias por cualquier ayuda.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Supongo que está llamando a ese método, por ejemplo, en Update .

Simplemente puede mantener una referencia al último objeto de impacto y deshabilitarlo como, por ejemplo

 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda