OnPointerUp not firing

So I am unable to actually get the Unit Spawner to place on the floor. I found a few articles online that state that OnPointerUp ONLY works when it is over the object OnPointerDown was clicked on. I tested this by letting go of the mouse button over the Building Button and sure enough it drops one there. Oddly though my Debug.Log() calls never fire even when over the Building Button. I have checked against the Git repo and actually copy clipped it into my script and I get the same result I was getting before…unable to place a building. I have made sure the UnitSpawner is in the Buildings array and is Id 1, I have made sure the UnitBase was Id 0, I have checked that the floorMask is applied to the floor…I have spent hours trying to fix this and rewatching the video and I can’t figure out how he gets the Unit Spawners to place. Any suggestions would be helpful.

Hi there,

Where is the Debug.Log() that isn’t being triggered?

Is your building preview appearing when you click?

Hi Yitzchak,
Below is the code for OnPointerUp. I added the Debug.Log(); call right away so as soon as OnPointerUp was called it should have fired the Debug message. The OnPointerDown does so I know that works. It actually will place the building but ONLY when I reclick on the building button.

In the screenshot you can see the ones I placed that are near the building button. The one in the middle will not place on the floor.

Not shown in my code but I added Debug.Log(); calls to see if it there was any value for ray or building.GetId and I got nothing. I even tried another mouse but the same result.

public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("The mouse was clicked");
        if (eventData.button != PointerEventData.InputButton.Left)
        {
            return;
        }
        buildingPreviewInstance = Instantiate(building.GetBuildingPreview());
        buildingRendererInstance = buildingPreviewInstance.GetComponentInChildren<Renderer>();

        buildingPreviewInstance.SetActive(false);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("mouse button is released");
        if (buildingPreviewInstance == null) { return; }

        Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

        if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, floorMask))
        {
            player.CmdTryPlaceBuilding(building.GetId(), hit.point);
        }

        Destroy(buildingPreviewInstance);
    }

Please go to Project Settings -> Player -> Other and make sure the Active Input Handling is set to both. Sometimes there is a bug where Unity resets this so make sure it is correct if you are still getting the error. Let me know if this helps.

Yeah it is set to both. It didn’t change and I didn’t change it. Still have the same issue that it won’t recognize OnPointerUp. Update: I tried changing it to New and to old and then back to both. It still won’t work. It has to be a setting somewhere but I have no idea what.

Okay, some other things to check.
Do you have the IPointerUpHandler interface declared for this class? Should be on the line with MonoBehaviour.

Do you have any colliders on your building preview? If so, remove them.

Make sure there is no Raycaster attached to the UnitHandler or it’s child canvas.

1 Like

Ok…so I checked a couple days ago if I had IPointerUpHandler declared and sure enough I did. I became frustrated as I have tried to move on in the course but not being able to place a building…well it just felt pointless. I went back and took a look today trying not to feel discouraged. I checked that I had declared both the up and the down pointer handlers…I had…but!!! It took a fresh set of eyes but I noticed I had a spelling mistake in the IPointerUpHandler. I didn’t receive any feedback that it wasn’t valid so I never gave it another thought. I corrected the mistake and voila! It works!

Thank you so much Yitzchak for your willingness to help and your patience. I truly appreciate it as now I am re-energized and ready to continue on with the course!
Cheers!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms