- Home /
Help With OnMouseOver and object Selection
I am attempting to build a matching game where you click two objects that flip over and if they match they are deleted, if they don't then they flip back over. I am having an issue with the code that controls the selection; mainly it's not working at all. when i run the game the debug to show that i clicked only shows up once about every 5 clicks; on top of that it doesn't show what is being hit with the raycast nor is it setting the variable in the Game Master script. I could really use a few extra eyes to figure out what i have missed/don't understand. any help would be appreciated.
using UnityEngine;
using System.Collections;
public class Flip : MonoBehaviour
{
public GameObject gameMaster;
private Vector3 mousePos;
public GameObject obj;
// Use this for initialization
void Start ()
{
obj = null;
}
// Update is called once per frame
void Update ()
{
mousePos = Input.mousePosition;
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
void OnMouseOver ()
{
var _selection1 = gameMaster.GetComponent<GameMaster> ().selection1;
var _selection2 = gameMaster.GetComponent<GameMaster> ().selection2;
obj = GetComponent<Collider>().gameObject;
//Debug.Log("Hovering over: " obj.name);
if (Input.GetMouseButtonDown (0))
{
Debug.Log("click");
if(_selection1 == null)
{
_selection1 = obj;
}
else
{
if(_selection2 == null)
{
_selection2 = obj;
}
else
{
return;
}
}
}
}
}
You didnt give the direction for the Raycast properly..
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
i actually fixed this but it giving me other issues so...
Your answer
![](https://koobas.hobune.stream/wayback/20220613200840im_/https://answers.unity.com/themes/thub/images/avi.jpg)