- Home /
Some Copies of Prefab Detect Clicks But Others Don't
I have several copies of a prefab representing cities in my game, and I want the GUI to update when one is selected. The problem is that some respond while others don't even though they have the same script and hit boxes. I've tried both OnMouseDown and OnMouseOver with mouse input variations of selection.
void OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
selected =! selected;
}
}
With the OnMouseOver method, I added a temporary Debug.Log to print the object's name if the mouse was over the object. It worked even for the objects that were not detecting clicks.
Inspector of click detecting copy:
Inspector of non-click detecting copy
Thank you in advance. This issue really effects playability.
maybe the On$$anonymous$$ouseXXX events and Input.GetXXX methods don't play well together.
Anyway, why not just using On$$anonymous$$ouseDown() without the Input? On$$anonymous$$ouseDown already indicates a click on the object.
$$anonymous$$aybe your mousebutton is malfunctioning? :p
Doubt it, given that my mouse works everywhere else.
what happens when you hold your click with Input.Get$$anonymous$$ouseButton(0)?
Answer by robertbu · Aug 15, 2013 at 04:04 AM
Often when you have this kind of problem, it is because there is some other collider in the way or there is a problem with the collider. Put this script on an empty game object and then click on any of the cities.
#pragma strict
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) {
Debug.Log("name: " + hit.collider.name + " tag: " + hit.collider.tag);
}
}
}
If it does not hit at all, then there is likely an issue with the collider...not size right, or the normals or facing the wrong direction. If it hit something else besides a city, then you need to address the problem.
I tried the script and it is registering the collision on all the cities correctly, so the hit boxes are working. I'm even more confused about what the problem is now.
The script you gave is very versatile and I will probably end up using it several times in the future, so I thank you in advance.