• 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
0
Question by birns92 · Apr 28, 2016 at 02:26 AM · script.meshcolliderhighlight

Keep Object Highlighted until another object is selected.

I would imagine this has a very simple solution but I can't find It. In my example I click the block on the right to highlight it. alt text

after I release left click the highlight is removed, I want the block on the right to stay highlighted until I click the block on the left like in this example.

alt text

Both blocks have the same script attached to them. I use:

 private float blueMultiply = 3.50f;
 private float redGreenMultiply = 0.50f;

 private Color originalColor;

 private void Start()
 {
     originalColor = gameObject.GetComponent<Renderer> ().material.color;
 }

 void OnMouseDown()
 {
     AddHighlight();
 }

 void OnMouseUp()
 {
     RemoveHighlight();
 }

 private void  AddHighlight()
 {
     float red = originalColor.r * redGreenMultiply;
     float blue = originalColor.b * blueMultiply;
     float green = originalColor.g * redGreenMultiply;

     gameObject.GetComponent<Renderer> ().material.color = new Color(red, green, blue);
 }

 private void RemoveHighlight ()
 {
     gameObject.GetComponent<Renderer> ().material.color = originalColor;
 }

How can I remove the highlight only when another gameobject, in this case the other block is clicked. So I don't have to use onMouseup to remove the highlight

block-2.jpg (6.8 kB)
block.jpg (8.0 kB)
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

2 Replies

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

Answer by sk8terboy4 · Apr 28, 2016 at 10:15 PM

You just need to get the reference of the other object in the script:

 public GameObject otherObj;
     
     void OnMouseUp(){
             AddHighlights(); // adds a highlight to this gameobject
             otherObj.RemoveHighlight(); // remove the highlight from the other gameobject
     }
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 birns92 · Apr 28, 2016 at 10:54 PM 1
Share

Sweet, that was simple! I reworked your code slightly to make it work, but the information you provided is what I needed. Ill add my new script as an answer as a reference for anyone who may look at this later. Thanks!

avatar image
1

Answer by birns92 · Apr 28, 2016 at 11:00 PM

Using @sk8terboy4's information I just brushed up the script for the desire result. Here it is:

 public GameObject otherobj;
 private float blueMultiply = 3.50f;
 private float redGreenMultiply = 0.50f;

 private Color originalColor;

 private void Start()
 {
     originalColor = gameObject.GetComponent<Renderer> ().material.color;
 }

 void OnMouseDown()
 {
     AddHighlight();
     RemoveHighlight();
 }

 private void  AddHighlight()
 {
     float red = originalColor.r * redGreenMultiply;
     float blue = originalColor.b * blueMultiply;
     float green = originalColor.g * redGreenMultiply;

     gameObject.GetComponent<Renderer> ().material.color = new Color(red, green, blue);
 }

 private void RemoveHighlight ()
 {
     otherobj.GetComponent<Renderer> ().material.color = originalColor;
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

43 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 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 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 avatar image

Related Questions

Keep gameobject highlighted until another gameobject is selected (multiple gameobjects) 2 Answers

How can i highlight objects with the mouse when the mouse cursor is moving over objects ? 1 Answer

if button is highlighted run script (non-mouse) 0 Answers

Endless Staircase 1 Answer

Question about the underlying source core of Cinemachine in Unity2018 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