Highlight and Select Object at Runtime Using C# in Unity

preview_player
Показать описание
Highlight object on mouse hover and select it on mouse click using Raycast in runtime Unity.

C# script to allow assigning a different material when the mouse hovers over or selects an object and assigns the original material back on the mouse out.

It recognises UI elements and disables selection when the mouse is over them.

Deselect the object when selecting another one or click on an empty space.

Determine which objects are selectable and which are not.

Link to Selection code on GitHub:
Рекомендации по теме
Комментарии
Автор

thanks this actually helped me solve a very unique problem.

mrlegoboy
Автор

Why was a new GameObject created instead of manually adding components to existing objects?

Nurasik
Автор

how do i modify the script if i do not want to tag the object as selectable and would only need to put the script to a specific object and have its own highlight material?

tomoya
Автор

How would you change the script if we swap the mouse with the controllers from the Meta Quest Pro?

agurez
Автор

how to select the game object even if I click multiple times on the same game object. In this tutorial, if I click double times on the same game object the selected game object will get deselected. is there a way if I click on the game object the clicked game object should be selected until I click on another thing or clicking on a different part of the screen.

rafeeqpasha
Автор

Awesome video, thank you! The only problem I had that I couldn't select meshes I generated with a script, what do I need to add to that mesh to be able to interact with rays?

kristofjakovac
Автор

I'm getting this error message: Assets\StarterAssets\TutorialInfo\Scripts\Editor\ReadmeEditor.cs(53, 14): error CS0117: 'Selection' does not contain a definition for 'objects'

How do to fix it?

Lodreus
Автор

Ray should be casted from FixedUpdate because physics

Alyson
Автор

Hi!
Thanks for the tutorial, it really helped me out. Do you have any suggestions about distinguishing the selectable objects? Like if i'm selecting two different typess of buildings to give different stats? Right now i'm trying with a script that makes me able to use multiple tags, but a more simple option would really help me out. Thanks! :)

benitajthy
Автор

Is it possible to have multiple objects selected?

atlas
Автор

This only works for me if I take the "!" out of the if statements e.g., ---

instead of

if && Physics.Raycast(ray, out raycastHit))

I have

if && Physics.Raycast(ray, out raycastHit))

have I set up the EventSystem wrong or something?

matthewegbert
welcome to shbcf.ru