I have been searching a lot of related questions but I still couldn't figure out why my OnMouseUp() is not working.
I want to use the script Mover to raise the gameobject up when it is selected with a left click on the object and cancel it with a right click anywhere.
Here is the information of the gameobject. It does have a collider, so I don't know where I did wrong.
Here is the code of Mover.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour {
private bool selected;
// Use this for initialization
void Start () {
selected = false;
}
// Update is called once per frame
void Update () {
Vector3 pos = GetComponent<Rigidbody>().position;
if (Input.GetMouseButtonUp(1) && selected)
{
GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y - 1f, pos.z);
selected = false;
}
}
void OnMouseUp()
{
Vector3 pos = GetComponent<Rigidbody>().position;
if (!selected)
{
GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y + 1f, pos.z);
selected = true;
}
Debug.Log("OnMouseUp!");
}
}
The debug.log isn't showing up as well.
---Update---
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Mover : MonoBehaviour, IPointerEnterHandler
{
private bool selected;
void Start () {
selected = false;
addPhysicsRaycaster();
}
void Update () {
Vector3 pos = GetComponent<Rigidbody>().position;
if (Input.GetMouseButtonUp(1) && selected)
{
GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y - 1f, pos.z);
selected = false;
}
}
void addPhysicsRaycaster()
{
PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
}
}
public void OnPointerEnter(PointerEventData eventData)
{
Vector3 pos = GetComponent<Rigidbody>().position;
if (!selected)
{
GetComponent<Rigidbody>().position = new Vector3(pos.x, pos.y + 1f, pos.z);
selected = true;
}
Debug.Log("OnPointerEnter!");
}
}