# Randomness in Click Event

**URL:** <https://community.cesium.com/t/randomness-in-click-event/33806>\
**Category:** CesiumJS\
**Created:** [July 15, 2024, 1:50pm UTC](https://community.cesium.com/t/randomness-in-click-event/33806 "2024-07-15T13:50:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![swapnil.k00](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/swapnil.k00/32/2920_2.png) [@swapnil.k00](https://community.cesium.com/u/swapnil.k00)\
**Post date:** [July 15, 2024, 1:50pm UTC](https://community.cesium.com/t/randomness-in-click-event/33806/1 "2024-07-15T13:50:03Z")

</div>

Using latest version of Cesium 1.119  
With no options for viewer object.  
In below code, I’m drawing around 2000 entities. Just polygon boxes.  
And trying to add Click event on it just to highlight it.  
But when i do that, it just selects random entities. But it does happen in pattern, e.g. like same 4- entities will be selected if I continue clicking at one point.

```auto
viewer = new Cesium.Viewer('cesiumContainer');
.
.
.
.
$.each(AllBuildingDetails, function (index, EachBuilding) {
				if(typeof EachBuilding != "undefined")
				{
					ent = viewer.entities.add({
						id: "bldg-"+EachBuilding.id,
						polygon : {
							hierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(eval("["+EachBuilding.LatLonCoordinatesString+"]"))),
							material : Cesium.Color.fromCssColorString("#ff0000").withAlpha(0.5),
							classificationType : Cesium.ClassificationType.BOTH
						}
					});
				}
			});

var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
		handler.setInputAction(function(click) {
			var pickedObject = viewer.scene.pick(click.position);
			console.log("pickedObject");
			console.log(pickedObject);
			console.log(pickedObject.id._id);
			if(lastSelectedId != null)
				viewer.entities.getById(lastSelectedId).polygon.material = Cesium.Color.RED.withAlpha(0.5);
			
			viewer.entities.getById(pickedObject.id._id).polygon.material = Cesium.Color.RED.withAlpha(1);
			lastSelectedId = pickedObject.id._id;
			
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

```

Here is what is happening  
 ![image](https://global.discourse-cdn.com/cesium/original/3X/3/a/3a0bf2e9af8923260714803c1fb1e322f453e65c.png)

---

<div class="post-metadata">

**Author:** ![yhexin0921](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yhexin0921/32/26025_2.png) [@yhexin0921](https://community.cesium.com/u/yhexin0921)\
**Post date:** [October 9, 2024, 8:43am UTC](https://community.cesium.com/t/randomness-in-click-event/33806/2 "2024-10-09T08:43:56Z")

</div>

他目前确实有这个问题，我也在做和你相同的事情，目前我只能通过getPickRay提高精度也是不能100%解决问题。期待后续能解决这个问题。

---

<div class="post-metadata">

**Author:** ![Luke\_McKinstry](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/luke_mckinstry/32/25557_2.png) [@Luke\_McKinstry](https://community.cesium.com/u/Luke_McKinstry)\
**Post date:** [October 9, 2024, 5:44pm UTC](https://community.cesium.com/t/randomness-in-click-event/33806/3 "2024-10-09T17:44:09Z")

</div>

Hi @yhexin0921 ,  
Thanks for your post and welcome to the Cesium community.  
Apologies @swapnil.k00 that you seem not to have gotten a response to the original post in this thread, but hopefully this conversation will help you too if you are still working on the issue.

I am not totally able to tell what is happening from the attached screenshot. Is the picking error happening in the same way at all zoom levels?

One possibility is that multiple objects overlap and `Scene.pick` [docs link](https://cesium.com/learn/cesiumjs/ref-doc/Scene.html?classFilter=Scene#pick) is returning the top one. From the docs:

> Returns an object with a `primitive` property that contains the first (top) primitive in the scene at a particular window coordinate or undefined if nothing is at the location.

Please let me know if that is not helpful, some additional details would also be useful.  
Thank you,  
Luke
