OnMouseOver not responding to mouse clicks

For some reason, OnMouseOver does not always respond to mouse clicks. Could someone understand why? OnMouseDown doesn’t respond to mouse clicks either and when I put the code in an Update method all the towers that can be placed are all placed at once.

@David_Turull Try putting a Debug.Log(other.collider) to see what the OnMouseOver is doing when you mouse over things

Lloyd Risper

A simple print statement like:

print("Mouse over" + gameObject.name);

prints the correct blocks when placed in OnMouseOver.

1 Like

Here is what I have in OnMouseOver:

private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (isPlaceable)
            {
                print("Mouse clicked on " + gameObject.name + " which you can place a tower ");
                Instantiate(towerprefab, transform.position, Quaternion.identity);
                isPlaceable = false;
            }
            else
            {
                print("Can't place here");
            }
        }
        print("Mouse over" + gameObject.name);

    }

And the only thing that is run is the " Mouse over" print statement.

Edit: I had this code in the Waypoint script but since it wasn’t working I chose a different block to put the towers and then the OnMouseDown event worked. Here is the solution I came up with:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TowerPlacement : MonoBehaviour
{
    [SerializeField] Tower towerprefab;

    private void OnMouseDown()
    {
        var placementPosition = transform.position;

        placementPosition.y += 10;

        Instantiate(towerprefab, placementPosition, Quaternion.identity);
   }
}

I put this script on the neutral blocks that I was not using as a waypoint.

Edit #2: After further testing I saw that I had an extra mesh collider from a previous tutorial lesson that was interfering with the calls to OnMouseOver on the other box collider so I deactivated it.

1 Like

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

Privacy & Terms