- Home /
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);
}
}
}
}
}
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.
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.
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;
}
}
}
}
}
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
Follow this Question
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