0
votes

Since the keypad don't have buttons I added cubes to each number and attached a script to each cube so when clicking on a cube it will show it's number in the top area.

For example Key 1 (Cube) when I click on it when the game is running it's not firing the click on all the cube area same for all the other cubes :

Key 1

When I click on the cube of key 1 it's working only on the left bottom area mostly on that sides. Even if the cube is covering the whole key area.

This is how it looks like when I uncheck the cubes mesh renderer :

Cubes when mesh renderer is unchecked

Then when running the game and clicking on a cube : it's showing the number in the Security Keypad Text(Text Mesh)

Clicked on a cube when the game is running

This is the script attached to each cube :

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public class SecurityKeypadKeys : MonoBehaviour
{
    public TextMesh text;

    private void Start()
    {
        text = GameObject.Find("Security Keypad Text").GetComponent<TextMesh>();
    }

    private void OnMouseDown()
    {
        string[] digits = Regex.Split(transform.name, @"\D+");
        foreach (string value in digits)
        {
            int number;
            if (int.TryParse(value, out number))
            {
                text.text = value;
            }
        }
    }
}

The main problem is that on most of the cubes you need to click on specific point/small area to make it work and I want it to be working trigger when you click on each cube at any place on the cube.

1

1 Answers

0
votes

Try the RaycastExample by robertbu code here and put it on a script. OnMouseDown() requires nothing else be in the way. That code will tell you if something is in the way. Also, try moving everything closer as OnMouseDown() has a depth limit to its raycast.

I recreated your above example, and it works perfectly, so something else must be wrong.