- Home /
Clickable object not properly working
Hello guys, so I have 3 clickable objects in my scene, they do the work that I want to be done but they are not properly working. What I mean but that, when I move my character and click it sometimes it does not work and you have to go further with the character and click it again. I also tried to make the sphere collider radius larger but it did not fix the problem. Here is my scirpt:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonOnClickActive : MonoBehaviour {
public GameObject canvas;
private bool clicked;
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0) && !clicked)
{
canvas.SetActive(true);
clicked = true ;
GetComponent<Renderer>().material.color = Color.red;
}
}
}
Any thoughts?
Bit hard to say. Is this the whole script? Are there any other scripts attached? Is the Collider ever deactivated? Try giving some more data on this as it should just work.
$$anonymous$$ost likely, some other collider is in between your view and the target so then there is no mouseover being registered by the collider you are trying to get. That would explain why moving handles is.
Try using some Debug.Log() statements in your On$$anonymous$$ouseOver() function to see how often it is called (not including the if statement).
So you want it to work like a button? Or do you want it to be a clickable object that can only be clicked when the player faces it?
Answer by KittenSnipes · Sep 28, 2018 at 02:16 AM
@stylianos-theodorikakos Well I got a script that works. I explain what is going on through comments in the script. Hope it helps.
[Header("Canvas related to clicked object")]
public GameObject canvas;
[Header("Needed to get the mouse position of the player")]
public Camera cam;
[Header("How far a player can hit an object from")]
public float reachOfPlayer = Mathf.Infinity;
//Variables:
private bool clicked = false;
private Color originalColor;
private void Update()
{
//If left clicked
if (Input.GetMouseButtonDown(0))
{
//Perform our click function
ClickedOnObject();
}
}
void ClickedOnObject()
{
//Will hold the reference for the clicked object
RaycastHit hit;
//Find the position and direction from the mouse
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//Using the direction and origin from the mouse(ray)
//Output the hit object (out hit)
//Max Distance the ray can go until it hits an object(reachOfPlayer)
if (Physics.Raycast(ray, out hit, reachOfPlayer))
{
//If the hit object is the parent of this script and it has not been clicked
if (hit.transform.gameObject == gameObject && clicked == false)
{
//Turn on our canvas
canvas.SetActive(true);
//Make sure we set our object as clicked
clicked = true;
//Get our original object color
originalColor = GetComponent<Renderer>().material.color;
//Then change it to the selected color
GetComponent<Renderer>().material.color = Color.red;
}
//If the hit object is the parent of this script and it has been clicked
else if (hit.transform.gameObject == gameObject && clicked)
{
//Turn off our canvas
canvas.SetActive(false);
//Make sure we set our object as not clicked
clicked = false;
//Then change our object's color back to its original color
GetComponent<Renderer>().material.color = originalColor;
}
}
}
@$$anonymous$$ittenSnipes man thank you very much for your effort. Unfortunately it works like before it is like the character blocks the clicks
Because it does. What you want to do for that is either use a seperate layer to hold the player and have the raycast ignore the collision of that layer or you can use some other method :D
Your answer
Follow this Question
Related Questions
Slowing or putting a timer on script 0 Answers
How can i convert this javascript line to csharp ? 1 Answer
Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer
Is their any way to save a particle system when it's not instantiated yet? 2 Answers
Point Counter Works Only Once! 1 Answer