Raycasting + setActive()
I'm basically trying to make an outline of an object by having a duplicate of the object as a child of the object. When the cursor is over the original objectthan the duplicate will then appear (duplicate will be a bit behind the original and a tad bigger with a solid -colour material). I'm having trouble making this work. As of this moment i have...
private RaycastHit hit;
private Ray ray;
public string otherObjectName;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
bool didRayHit = Physics.Raycast(ray, out hit);
if(didRayHit == true && hit.collider.name == otherObjectName)
{
gameObject.SetActive(true);
Debug.Log ("in");
}
else
{
gameObject.SetActive(false);
Debug.Log ("out");
}
}
When i hover over the original object nothing changes. Not quite sure what i'm doing wrong and any help here will be appreciated.
Just something i noticed:
The logs: At the beginning (when i'm not hovering over the original) it'll print "out", which is fine. When i hover over the original it'll print "in", which again is fine. However, when i hover out of the original it'll still be stuck on "in" and will not change again.
If i change the SetActive() in the if statement to SetActive(false) and then have SetActive(true) in the else it'll work fine (in reverse) but only once (when i hover over the original the duplicate will disappear but won't appear again even if i hover over the original)
Answer by Tycellent · Aug 31, 2015 at 11:09 AM
Ahh i solved the problem. it's because setactive() basically deactivates the object. i should be using:
rend = GetComponent<Renderer>();
rend.enabled = false;
I believe this is why it didn't work previously. I could be wrong but it works as intended now so it's all good.
:)