Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by jukke · Mar 26, 2012 at 03:00 PM · raycasthighlighthover

Best way to "highlight" an object on mouse over

Lets say I have like 25 different objects in the scene which are always visible for the camera with quite complex colliders. I then want to highlight whatever object im hovering hover(with the mouse) by changing the texture or whatever. Which is the best way to do this?...I guess I could send out raycasts every frame and change the texture whenever it hits something, but that seems quite expensive?, is there any other way?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image farooqaaa · Mar 26, 2012 at 03:09 PM 0
Share

How about changing the material color?

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by raoz · Mar 26, 2012 at 03:51 PM

This will be attached to all the things that can be higlighted with hovering.

 private Color startcolor;
 void OnMouseEnter()
 {
     startcolor = renderer.material.color;
     renderer.material.color = Color.yellow;
 }
 void OnMouseExit()
 {
     renderer.material.color = startcolor;
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mcnamaraali · Aug 28, 2013 at 02:02 PM 0
Share

color.yellow won't work for me. I've tried color.#... I've tried color.antiqueBlue; etc etc you get where I'm going here. I just want to highlight a gameObject On$$anonymous$$ouseClick or another event perhaps!

The rest of the script seems fine in $$anonymous$$onoDevelop for Unity. Including caps where it's supposed to be caps etc.

Could someone help with this please?

avatar image masterlewis02 · May 18, 2014 at 09:50 PM 3
Share

using UnityEngine; using System.Collections;

public class textcolour : $$anonymous$$onoBehaviour {

 private Color startcolor;
 void On$$anonymous$$ouseEnter()
 
 {
     startcolor = renderer.material.color;
     renderer.material.color = Color.red;
 }
 void On$$anonymous$$ouseExit()
 {
     renderer.material.color = startcolor;
 }

}

this is what i tried but it didnt work!! what did i do wrong?

avatar image Loius · May 19, 2014 at 12:51 AM 0
Share

you need a renderer with a material and a collider for the mouse to see. do you have errors? that code should work.

avatar image domiii · Jun 25, 2015 at 10:47 AM 0
Share

Can anyone with sufficient privileges fix the original answer's typos? The type color does not exist; should be Color.

avatar image raoz · Jun 26, 2015 at 09:53 AM 0
Share

I fixed it.

Show more comments
avatar image
2

Answer by astracat111 · Feb 19, 2018 at 03:25 AM

Okay so your problem with OnMouseEnter and Exit is that you are setting yourself up to be confused if you are porting to a VR platform, or a platform that has a pointer controller. If you want to keep your project open ended and not only have it releasable for PC, you can do something like below:

Create a main game object that doesn't get destroyed so that you have a scene manager object between scenes. Make a main game script, we will prtend it's called "MainGameScript.cs" and it's class name is 'MainGameScript' and put this inside of it, above it's class:

 [System.Serializable]
 public class MainInput_Ray { 
 public Ray ray = new Ray ();
 public RaycastHit hit;
 public bool hittingSomething { get; set; }
 }

Then goes in your main game script's start:

 public MG_Ray mainInput_Ray;

 Start() { 
     mainInput_Ray = new MainInput_Ray();
 }

...and then this goes in your main game script's update AFTER the scene is fully loaded and playing:

 Update() { 
     mainInput_Ray.ray = MG_Camera.GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
 mainInput_Ray.hittingSomething = Physics.Raycast (mainInput_Ray.ray, out mainInput_Ray.hit, 1000);
 }

Now in your object's script you can go like:

 private MainGameScript mainGameScript;

 Start() { 
     mainGameScript = GameObject.Find("MainGameScript"); //gameobject find is okay if it's in your start method
    //make sure you set your main game script's script execution order to FIRST
 }

 Update() { 
     if (mainGameScript.mainInput_Ray.hittingSomething && (mainGameScript.mainInput_Ray.hit.transform == this.transform.parent.transform)) {
  
    }
 }

Then throughout your entire project whenever you need to check if something is hit you do mainInput_Ray.hittingSomething == true?????, check the results, throw an && in the if statement, just make sure the scene if fully loaded. Personally I have a state machine in my main game object that handles the loading screen, saving and loading of data and has a 'SceneActive' state I check against first.

Now if you have a VR controller you just have to replace this part:

         mainInput_Ray.ray = MG_Camera.GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);

This code is optimized just like OnMouseEnter, because you aren't creating a new ray all together each and every frame, there is only one global ray you check against.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jkilla1000 · Mar 26, 2012 at 03:23 PM

function OnMouseEnter?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HalversonS · Apr 09, 2014 at 05:20 PM 0
Share

function On$$anonymous$$ouseOver() is Javascript. They're using C# above.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hello! I'm trying to make it so when i hover over a GameObject (TV), it affects other GameObjects' textures. 0 Answers

rotating hovercar to be at the same angle as the terrain/object below it 1 Answer

Raycast MouseOver: How to get it to go away when mouse is not over 2 Answers

How to determine what points on terrain can be seen from a certain point? 1 Answer

Make camera hover at 50 over hilly terrain 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges