Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by $$anonymous$$ · Dec 12, 2017 at 04:36 AM · raycastingrayhitselecthighlight

Problem highlighting object?

Hello, I made a script to highlight an object white so that I can select it, but it does not seem to be working at all; I assume the error is obvious, but I am not very good at this sort of thing.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HighlightObjectScript : MonoBehaviour {
 
     Ray Ray1;
     RaycastHit Hit1;
 
     public GameObject Highlight;
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             if (Physics.Raycast (Ray1, out Hit1)) {
                 if (Hit1.transform.gameObject == Highlight){
                     Hit1.transform.gameObject.GetComponent<Material> ().SetColor(Hit1.transform.gameObject.GetComponent<Material> ().name, Color.white);
                 }
             }
         }
     }
 }
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
1
Best Answer

Answer by sethuraj · Dec 12, 2017 at 05:18 AM

Hi David,

The code you written is insufficient and missing some lines. If you are a beginner in Unity,I realy suggest you go through some more tutorials. For this case I added the missing lines and commented them so that you can understand.

 public class HighlightObjectScript : MonoBehaviour
 {
     //Need to drag and drop the target object (with a collider) on this
     public GameObject m_TargetObject;
     //The variable to store the current hitting object
     GameObject m_CurrentObject;
 
     void Update()
     {
         //If mouse is held down
         if (Input.GetMouseButton(0))
         {
             //Cast a ray from mouse position to world
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             //The ray cast hit result
             RaycastHit hitResult;
 
             //If the ray hit something
             if (Physics.Raycast(ray, out hitResult))
             {
                 //Try to get the hited object
                 m_CurrentObject = hitResult.collider.gameObject;
 
                 //If an object available and its matching our target object, change its color to red
                 if (m_CurrentObject && m_CurrentObject == m_TargetObject)
                     m_CurrentObject.GetComponent<MeshRenderer>().material.color = Color.red;
             }
         }
         //Else if mouse is released
         else
         {
             //If last object still available
             if(m_CurrentObject)
             {
                 //Reset its color back to white
                 m_CurrentObject.GetComponent<MeshRenderer>().material.color = Color.white;
                 //State we are not hitting any object
                 m_CurrentObject = null;
             }
         }
     }
 }

Dont forget to drag and drop the object you wanted to detect/highlight to the 'TargetObject' field in the inspector of your script.

Hope this helps.

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 $$anonymous$$ · Dec 12, 2017 at 09:58 PM 0
Share

What does it mean when you do something like if ($$anonymous$$_CurrentObject){}? I'd think that that would just give an error if the object does not exist. Also, I'd like the color of the object to go back to whatever it originally was before it was selected if that is possible.

avatar image $$anonymous$$ · Dec 12, 2017 at 10:17 PM 0
Share

Here Is the code I am currently using, the problem is that it does not turn an object back to its original color after clicking on a different object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HighlightObjectScript : $$anonymous$$onoBehaviour {
 
     public GameObject CurrentObject;
 
     private Color OriginalColor;
 
     void Update () {
         Ray TheRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit HitResult;
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             if (Physics.Raycast (TheRay, out HitResult)) {
                 CurrentObject = HitResult.transform.gameObject;
                 if (CurrentObject.tag == "Highlight") {
                     if (CurrentObject.GetComponent<$$anonymous$$eshRenderer> () != null && OriginalColor != Color.black)
                         OriginalColor = CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color;
                     CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color = OriginalColor + Color.white/4;
                 }
             } else {
                 if (CurrentObject != null) {
                     CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color = OriginalColor;
                     OriginalColor = Color.black;
                     CurrentObject = null;
                 }
             }
         }
     }
 }

avatar image $$anonymous$$ $$anonymous$$ · Dec 12, 2017 at 10:48 PM 0
Share

Alright, I pretty much figured it out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HighlightObjectScript : $$anonymous$$onoBehaviour {
 
     public GameObject CurrentObject;
 
     private Color OriginalColor;
 
     void Update () {
         Ray TheRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit HitResult;
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             if (Physics.Raycast (TheRay, out HitResult)) {
                 if (HitResult.transform.gameObject.tag == "Highlight") {
                     if (CurrentObject == null) {
                         if (HitResult.transform.gameObject.GetComponent<$$anonymous$$eshRenderer> () != null) {
                             CurrentObject = HitResult.transform.gameObject;
                             OriginalColor = CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color;
                             CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color = OriginalColor + Color.white / 4;
                         }
                     } else {
                         CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color = OriginalColor;
                         CurrentObject = HitResult.transform.gameObject;
                         CurrentObject.GetComponent<$$anonymous$$eshRenderer> ().material.color = OriginalColor + Color.white / 4;
                     }
                 }
             }
         }
     }
 }

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

75 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 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

How to use raycast on generated objects. 0 Answers

Change raycasting 1 Answer

how to select an object with raycasting 1 Answer

Raycast Never Hits Anything?! 2 Answers

Need Help with RayCast. No Vector? 1 Answer


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