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 /
  • Help Room /
avatar image
0
Question by Thet Naing Swe · Sep 23, 2010 at 09:57 AM · objectmouseover

change the colour of the object when mouse is over the object

How can I change the material of the object if there is a mouse placed over that 3D object. I've seen the dragObject script but I don't want to use RigidBody although I am just going to use simple collider for that..

Comment
Add comment
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

1 Reply

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

Answer by · Sep 23, 2010 at 10:16 AM

An easy solution would be to use OnMouseOver. Example code to set to red when mouse is over:

var initialColor : Color;

function Start() { initialColor = renderer.material.color; }

function OnMouseOver() { renderer.material.color = Color.red; }

function OnMouseExit() { renderer.material.color = initialColor; }

Alternatively, you could raycast from the camera position to the mouse position. The benefit of the Raycast is that you can specify a layerMask (to only hit objects on a given layer).

function Update()
{
   var hit : RaycastHit;
   var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
   if ( Physics.Raycast( ray, hit, 100 ) )
   {
      if ( hit.collider.gameObject.renderer )
         hit.collider.gameObject.renderer.material.color = Color.red;
   }
}

In these examples, the color of the renderer material is explicitly set to red when the mouse is over it, but you can change it according to your needs.

Comment
Add comment · Show 3 · 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 Thet Naing Swe · Sep 23, 2010 at 01:39 PM 0
Share

How about this Camera Ray Casting in C# code ... I tried to convert it to C# but there has the problem with passing hit...

avatar image · Sep 23, 2010 at 01:41 PM 0
Share

In C# you need to add "out" before hit. i.e. if ( Physics.Raycast( ray, out hit, 100.0f ) )

avatar image Maverick87Shaka · Jan 29, 2018 at 12:30 PM 0
Share

Any idea on how to detect when raycast is no more hitting the same object? I was able to change color on "selection", each update while raycast is hitting the object the color is changed to the new one, but I'm looking in a efficient way to "restore" the initial color of the object like the On$$anonymous$$ouseExit().

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to determine class of object at mouse position 1 Answer

Start moving unchecks itself 0 Answers

how to make an object change size automatically when playing so it doesnt go through objects 0 Answers

Terrain to object? 0 Answers

My clone objects behind background 0 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