|
I made this in order to select objects : It works fine, and i know when the mouse goes over the object, but how could I do to know when the object is being unselected, when the mouse is leaving it? I mean, I want the object to change color when the mouse is over it. With that code I can only change the color when the raycast enters the object, but not when it leaves. So the object changes, but doesnt return back to the original color. thank you for all the help!
(comments are locked)
|
|
I think you should use OnMouseEnter and OnMouseExit instead. If the objects have a collider, these functions can do the job. Read about them in: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseEnter.html http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseExit.html Ok, I will read it but I thought that raycasting was the best way to select objects in 3d space. What do you think I should do? Thnx
Jun 24 '11 at 07:41 PM
Aer93
These functions already keep track of where the mouse pointer is, and which objects are "under" the pointer, so you should not need to do the raycast yourself. I've not tried them, but according to the docs they seem exactly what you're looking for.
Jun 24 '11 at 07:53 PM
aldonaletto
And then, when should I use the raycast?
Jun 24 '11 at 07:59 PM
Aer93
I think you don't need the raycast - at least to select the objects with the mouse. The docs contain examples on how to change the color of a object when the mouse is over it.
Jun 25 '11 at 12:32 AM
aldonaletto
(comments are locked)
|
|
You would have to cache the selected object somewhere. If you just want to change the color back it would be something like that: (untestet code) It doesnt work cachedObj.changeColorBack(); //unselect object Error: changeColorBack() is not a member of unity I tried to fix it like this var myAI2 : MyAI1 = cachedObj.GetComponent(MyAI1); myAI2.changeColorBack(); but now I get another error: cachedObj = MyAI1 Error: Cannot convert 'System.Type' to 'UnityEngine.GameObject' help
Jun 24 '11 at 07:38 PM
Aer93
changeColorBack() would be a custom function that you add to the MyAI1 script. Same as changeColor(), but now you should reset it to the old value. replace cachedObj = MyAI1 with cachedObj = object. That was my hasty typing, sorry :-) I edited the code above.
Jun 24 '11 at 08:31 PM
Mischa
I added the ChangeColorBack() function in MyAI1, but still the same problem Error: 'ChangeColorBack' is not a member of 'UnityEngine.GameObject'.
Jun 25 '11 at 06:26 AM
Aer93
(comments are locked)
|

I fixed your code formatting. PLEASE use the '010101' button to format your code in the future.
not sure I understand your question. Are you asking how to set a boolean to true of false if the raycast is hitting the object?
I want the object to change color when the mouse is going over it. With that code I can only change the color when the raycast enters the object, but not when it leaves.